`

HibernateTemplate Vs HibernateDaoSupport Vs Direct Hibernate Access

    博客分类:
  • java
 
阅读更多

http://forum.spring.io/forum/spring-projects/data/53831-hibernatetemplate-vs-hibernatedaosupport-vs-direct-hibernate-access
引用


It Depends...

Originally posted by sudheshnaiyer View Post
Also in our application, we need to support around 700 to 800 concurrent users. We were debating whether spring and hibernate are the right choices.


    The short answer is, yes, Spring and Hibernate are a great pair to start with. But a nuanced answer might be more helpful.

    Fundamentally, you're really asking a capacity question: does my application running on a given software stack scale feasibly?

        Scalability of the software stack -- Spring+Hibernate have been designed to scale, absolutely. These days, for new applications, Spring is a slam-dunk. Hibernate is very performant, itself, but like anything that works with the database, it's how you use it that makes the difference. But, yeah, it scales.
        Design of your app -- Ironically, this is the greatest source of performance (and thus capacity) problems. In general, application frameworks are developed with performance in mind. Doing things like querying the database repeatedly for the same data, or using lots of queries when one will do (so-called N+1 selects) or over synchronizing are the real source of performance problems. I would recommend spending less time debating about which framework to use and far more time digging in and learning how to use a given technology stack efficiently.
        Usage patterns of your app -- your application's performance is really about how efficiently it responds to what your users do. Let's say there's a page that users are hitting A LOT in a typical session; tuning the design of that part of the app so that it's cheap and fast will decrease how much it "costs" to service a typical user and thereby increase your capacity. Again, it's not about the frameworks, it's about the quality of the application code.
        Feasibility -- given all these factors, the final part is finding out how much hardware is required to handle the number of target concurrent users.
        Let's say you have a simple set-up: a single application server doing both front-end and back-end processing. You develop performance tests that simulate (as best you can) typical user actions on your site (complete with "think time" -- users sitting there reading the screen). Then you run that test repeatedly, ramping-up the # of users (even better if you randomize the start time for each user). All the while, you measure the response time (i.e. how long it takes from request to completed web page). Decide what's an acceptable response time (e.g. shopping pages are 1-5 seconds; check-out is 30-60 seconds). Once you start to exceed your acceptable response times, you check the number of concurrent users. That's how many users are supported by one application server. Let's say it's 300. Then in your case, you'll need 3 application servers (assuming the DB can handle that full load).
        There's lots of performance testing tools out there. I've had lots of success with Grinder: it's open-source, it gives you the tools you need to write meaningful tests and generate plenty of load, and it records the right metrics.

            Bring the framework debate to a conclusion, fast; then spend the balance of your time getting smart about how to efficiently write your app.

Originally posted by sudheshnaiyer View Post
I have seen in lot of forums and still in confusion. We are starting a new project and need to
decide whether we need to decide which strategy to use:

    Accessing Hibernate directly
    Using HibernateTemplate
    Using HibernateDAOSupport classes



    Taking a step back, when discussing using Hibernate within a Spring app, there are three core questions:

        Who will manage the lifecycle of the Hibernate Session?
        Who will manage the Transaction?
        How much of Spring's Hibernate-templating will you use in your DAOs?


    Lifecycle of the Hibernate Session
    To keep this simple, start with the goal of having a Hibernate Session open for the entire request. This is known as the "Open Session In View" which highlights the fact that the Hibernate Session remains open even in the view layer (e.g. inside your JSP that's rendering the response).

        Hibernate's Session Management
        Hibernate has a concept of "Contextual Sessions". Hibernate itself can store the current Session in one of two places for you: either on ThreadLocal or the JTA Session Context. Which place Hibernate uses is configurable (see section 2.5 in the Hibernate 3.3.1 Manual for instructions). This behavior is invoked when sessionFactory.getCurrentSession() is called. If a Hibernate Session doesn't already exist, one is created.

        The key here, is that in order for this mechanism to kick in, the calling code (either application code or Spring) must call sessionFactory.getCurrentSession().

        Spring's Hibernate Session Management
        Spring also has a strategy of storing the current Hibernate Session on ThreadLocal. However, it is using its own storage, separate from where Hibernate would store the Session.
        All of Spring's Hibernate supporting classes are aware of this storage:

            OpenSessionInViewFilter and OpenSessionInViewInterceptor -- which start a new Hibernate Session at the start of an HTTP request and puts it in this ThreadLocal storage. (The ...Interceptor variety is used within Spring's Web MVC framework).
            HibernateTransactionManager, HibernateTemplate, and HibernateDaoSupport -- all first check this ThreadLocal storage for a Hibernate Session; if none is found, creates a new one.

        In fact, if you configure the Hibernate SessionFactory as a Spring Bean (using Spring's LocalSessionFactoryBean), that FactoryBean will automatically hook-up Hibernate to use Spring's Session Storage (just described), thereby merging the two and allowing the co-mingling of both raw Hibernate API and Spring's Hibernate Support classes, seamlessly. (nice!)

    Therefore, the best approach to begin with is using the Spring-managed Hibernate Session approach. It is the responsibility/dominion of the application framework (here, Spring) to manage the lifecycle of such resources, anyway. If you DO choose to make use of Spring's support for Hibernate in your DAOs, the integration is seemless and easy. If you'd rather drop down and use the Hibernate API, directly, that's very easy to do too. So, you don't loose any flexiblity with this choice.

            In most web apps, let Spring manage the Hibernate Session: configure either the OpenSessionInViewFilter in your web.xml or the OpenSessionInViewInterceptor in your Spring Application Context.

    Transaction Management
    Again, both frameworks provide transaction management integration facilities. Without going into much gory detail, if you're already using Spring, it's a slam-dunk to use Spring to configure the transaction management.

            Use Spring's Transaction Management.

    DAO Authoring Style
    Given all this information to this point, hopefully it is clear that it merely becomes a question of style as to whether you use a HibernateTemplate, HibernateDaoSupport or straight Hibernate.

        Hibernate API
        The advantage of using the Hibernate API directly is that it will be familiar to those who already know Hibernate and you'll be able to use example snippets coded to this API.

        The disadvantage of use the Hibernate API is that you don't get the benefits of Spring's DataAccessException translation. Which, depending on your shop, you might care about this.

        Generally speaking, if you're committed to using Hibernate, this is not a bad choice.

        HibernateTemplate
        This is the first level of Spring's coding support for Hibernate. It eliminates the need for doing anything with the Hibernate Session, at all. It looks for a current Hibernate Session for you; if one exists, it executes the requested operation on that session. This is great for turning your simpler Hibernate code into one-liners.

        Also, Hibernate Exceptions are automatically converted to Spring's DataAccessException hierarchy.

        HibernateDaoSupport
        This is the second level of boilerplate removal Spring addresses with respect to Hibernate. This is a base class from which your DAOs extend. HibernateDaoSupport wraps a HibernateTemplate and includes the corresponding getter/setter definitions you would normally have to duplicate across your DAOs.

        Note that as of Java 1.5, with the inclusion of Generics, you can take this one step further and create a GenericDao.

            How you code your DAOs is really a question of style, but Spring's supporting classes genuinely reduces boilerplate code and are recommended.

I hope this helps. Good luck!


分享到:
评论

相关推荐

    hibernateTemplate和HibernateDaoSupport

    hibernateTemplate 和 HibernateDaoSupport 是 Spring 框架中针对 Hibernate 数据库访问层的两个重要组件,它们简化了基于 Hibernate 的数据操作,使得开发者能够更高效地进行 CRUD(创建、读取、更新、删除)操作。...

    简单封装 HibernateTemplate 各项功能

    GenericHibernateDao 继承 HibernateDao,简单封装 HibernateTemplate 各项功能,简化基于Hibernate Dao 的编写。

    spring-boot 集成hibernate

    这个类是Spring ORM模块提供的,它提供了一个便捷的`getHibernateTemplate()`方法,可以直接使用HibernateTemplate进行数据操作。但是,通常在Spring Boot中,我们更倾向于直接使用JPA的Repository接口,因为它们...

    Spring+hibernate整合源代码

    Spring 可以通过工厂方法或 JdbcTemplate、HibernateTemplate 提供的模板方法来获取 Session。 6. **Service 层设计**:Service 层是业务逻辑的核心,通常会注入 DAO 实例并调用其方法来完成业务处理。Service 层...

    Spring+Hibernate实现增删改查的两种方法

    本文将深入探讨如何利用Spring与Hibernate整合,通过两种不同的方式来实现这些基本操作:HibernateTemplate和HibernateDaoSupport。 **一、HibernateTemplate** HibernateTemplate是Spring提供的一个方便的类,它...

    Spring_Spring_Hibernate_HibernateTemplate

    《Spring与Hibernate整合:深入理解HibernateTemplate》 在Java企业级开发中,Spring框架和Hibernate持久层框架的结合使用是常见的技术选型。本篇将深入探讨Spring与Hibernate的集成,特别是Spring提供的...

    spring+hibernate整合

    - 配置 HibernateTemplate 或 HibernateDaoSupport,这两个类提供了对 Hibernate Session 的管理,使得业务层可以方便地调用。 3. **事务管理** - 在 `applicationContext.xml` 中配置事务管理器,如 `...

    hibernate_jar.rar

    DataSource通常配置数据库连接信息,SessionFactory用于创建和管理Hibernate会话,而HibernateTemplate或HibernateDaoSupport则为DAO层提供了便捷的数据库操作接口。 4. Hibernate JAR文件解析 "hibernate_jar.rar...

    对hibernate的封装 HibernateTemplate

    **标题:“对hibernate的封装 HibernateTemplate”** 在Java Web开发中,Hibernate是一个非常流行的对象关系映射(ORM)框架,它简化了数据库与Java对象之间的交互。然而,直接使用Hibernate API进行数据库操作可能...

    Spring,Hibernate整合源码

    4. **HibernateTemplate与HibernateDaoSupport**:Spring提供了HibernateTemplate和HibernateDaoSupport,它们为DAO层提供了便捷的操作方法,如save、update、delete、find等,同时处理了事务和异常。 5. **JPA集成...

    HibernateDaoSupport 与@Autowired

    首先,`HibernateDaoSupport`是Hibernate提供的一种用于简化DAO(Data Access Object)层开发的抽象基类。它属于`org.springframework.orm.hibernate5.support`包,为自定义DAO类提供了便捷的初始化和会话管理功能。...

    Spring3整合Hibernate4测试Demo

    4. **Spring的HibernateTemplate或HibernateDaoSupport**:Spring提供了HibernateTemplate和HibernateDaoSupport类,用于简化Hibernate操作。在这个Demo中,可能会使用它们来封装数据访问操作。 5. **Entity(实体...

    HibernateDaoSupport的使用

    总结,`HibernateDaoSupport`是Spring框架中为了简化Hibernate DAO层实现的一个重要工具,它通过提供SessionFactory注入、自动Session管理以及封装的HibernateTemplate,极大地提高了开发效率,降低了出错概率。...

    Hibernate各种查询:联表查询 分页查询 位置参数查询(?) 占位符查询(冒号查询) 统计查询

    在Java的持久化框架Hibernate中,查询操作是其核心功能之一。本篇文章将详细解析Hibernate中的几种主要查询方式,包括联表查询、分页查询、位置参数查询、占位符查询(冒号查询)以及统计查询,这些都是在实际开发中...

    Spring_2000_Spring_Hibernate_HibernateTemplate

    标题中的"Spring_2000_Spring_Hibernate_HibernateTemplate"显然指的是一个关于Spring框架、Hibernate ORM框架以及其在2000年左右的版本或应用方式的专题。在这个主题下,我们将深入探讨Spring框架的核心特性,以及...

    关于使用HibernateTemplate

    而`HibernateTemplate`作为Spring框架对Hibernate的一种封装,更是进一步降低了开发难度,提高了开发效率。 #### 二、核心价值:专注业务逻辑 `HibernateTemplate`的主要价值在于让开发者能够将更多的精力聚焦于...

    JavaEE spring和Hibernate整合(没有hibernate.cfg.xml)

    - Spring提供了`HibernateTemplate`和`HibernateDaoSupport`工具类,方便我们在Service层操作数据库。`HibernateTemplate`可以直接注入到Service类中,而`HibernateDaoSupport`需要我们继承,并在子类中注入`...

    spring 理解文件HibernateDaoSupport

    根据给定的信息,我们可以深入探讨Spring框架中与Hibernate集成的相关知识点,特别关注“HibernateDaoSupport”类及其在Spring环境中的应用。以下是对标题、描述以及部分文件内容的详细解析: ### 一、Spring与...

    hibernateDaoSupport类的运用实例

    `hibernateDaoSupport`是Spring框架中提供的一种支持Hibernate操作的基类。它主要用于简化Hibernate与Spring集成过程中的编码工作,使得开发人员能够更加专注于业务逻辑的编写而非框架间的耦合问题。本文将详细介绍`...

    Struts Hibernate Spring 集成开发宝典 tutorial

    2. **整合Hibernate和Spring**:Spring提供了HibernateTemplate和HibernateDaoSupport,用于简化Hibernate的使用。通过Spring的事务管理,可以在方法级别控制事务的开始、提交、回滚,避免了手动管理事务的复杂性。 ...

Global site tag (gtag.js) - Google Analytics