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!
发表评论
-
charles4.2下载与破解方法以及配置https
2020-02-26 09:03 2有两个抓包工具 一个是fidder,一个是charles,两个 ... -
序列号批量生成算法
2019-12-05 14:11 0业务处理过程当中,经常需要生成订单号、序列号等,简单的可 ... -
使用ANTLR处理文本
2019-08-28 17:32 762引用 使用 Antlr 处理文本 https://www.ib ... -
解决maven-metadata.xml文件下载卡死问题
2019-04-11 14:02 3975http://192.168.1.110:8081/nexus ... -
rsync备份和删除指定文件
2018-01-02 10:23 2044文件异地备份时,需要将本地文件合并到服务器上,且不能删除服务器 ... -
javaLocale格式化日期和数字
2017-08-25 09:26 864public static void main(Strin ... -
centos6 tomcat 启动脚本 tomcat服务
2017-08-23 11:24 1438系统自动启动tomcat 复制该脚本到/etc/init.d/ ... -
win7 命令行改IP和DNS
2016-12-21 18:35 731使用管理员权限运行CMD //改DNS netsh ... -
jenkins中集成sonar,使用findbug、pmd、checkstyle提升代码质量
2016-09-29 14:58 6166实际上jenkins单独也 ... -
jenkins 集成sonar
2016-09-18 10:14 0jenkins集成sonar可以从插件中心直接更新安装 son ... -
activeMQ5.14权限配置
2016-08-17 13:47 2669activeMQ默认的消息队列没有用户名和密码,可以直接通过T ... -
solaris 使用解压版的jdk
2016-07-27 15:17 761solaris上配置jdk其实也很简单 由于solaris有 ... -
solaris tomcat开机启动
2016-07-27 16:17 617创建文件夹/var/svc/manifes ... -
spring mvc mybatis will not be managed by Spring
2016-07-20 17:30 9876项目运行时发现事务提交不完整,回滚时只能回滚一半。 系统配置 ... -
java里判断一点是否在某个区域
2016-06-03 17:47 1830import java.awt.geom.Path2D ... -
12306的技术升级
2016-04-20 16:17 1026升级的核心是余票查询的升级,余票查询使用存储过程,sybase ... -
工作流的123
2016-04-20 12:58 570三分钟了解Activity工作流 工作流一般会给开发人员提供流 ... -
sping mvc 使用@Value注解为controller注入值
2016-04-17 17:39 10830spring mvc 里有两个配置文件, 第一个,为sprin ... -
googleapis.com域名访问慢的解决办法
2016-04-13 12:09 9681、安装火狐 2、安装插件ReplaceGoogleCDN -
ehcache-web缓存的使用和清除
2016-03-15 11:37 10510引入jar包 <!--ehcache缓存--> ...
相关推荐
hibernateTemplate 和 HibernateDaoSupport 是 Spring 框架中针对 Hibernate 数据库访问层的两个重要组件,它们简化了基于 Hibernate 的数据操作,使得开发者能够更高效地进行 CRUD(创建、读取、更新、删除)操作。...
GenericHibernateDao 继承 HibernateDao,简单封装 HibernateTemplate 各项功能,简化基于Hibernate Dao 的编写。
这个类是Spring ORM模块提供的,它提供了一个便捷的`getHibernateTemplate()`方法,可以直接使用HibernateTemplate进行数据操作。但是,通常在Spring Boot中,我们更倾向于直接使用JPA的Repository接口,因为它们...
Spring 可以通过工厂方法或 JdbcTemplate、HibernateTemplate 提供的模板方法来获取 Session。 6. **Service 层设计**:Service 层是业务逻辑的核心,通常会注入 DAO 实例并调用其方法来完成业务处理。Service 层...
本文将深入探讨如何利用Spring与Hibernate整合,通过两种不同的方式来实现这些基本操作:HibernateTemplate和HibernateDaoSupport。 **一、HibernateTemplate** HibernateTemplate是Spring提供的一个方便的类,它...
《Spring与Hibernate整合:深入理解HibernateTemplate》 在Java企业级开发中,Spring框架和Hibernate持久层框架的结合使用是常见的技术选型。本篇将深入探讨Spring与Hibernate的集成,特别是Spring提供的...
- 配置 HibernateTemplate 或 HibernateDaoSupport,这两个类提供了对 Hibernate Session 的管理,使得业务层可以方便地调用。 3. **事务管理** - 在 `applicationContext.xml` 中配置事务管理器,如 `...
DataSource通常配置数据库连接信息,SessionFactory用于创建和管理Hibernate会话,而HibernateTemplate或HibernateDaoSupport则为DAO层提供了便捷的数据库操作接口。 4. Hibernate JAR文件解析 "hibernate_jar.rar...
**标题:“对hibernate的封装 HibernateTemplate”** 在Java Web开发中,Hibernate是一个非常流行的对象关系映射(ORM)框架,它简化了数据库与Java对象之间的交互。然而,直接使用Hibernate API进行数据库操作可能...
4. **HibernateTemplate与HibernateDaoSupport**:Spring提供了HibernateTemplate和HibernateDaoSupport,它们为DAO层提供了便捷的操作方法,如save、update、delete、find等,同时处理了事务和异常。 5. **JPA集成...
首先,`HibernateDaoSupport`是Hibernate提供的一种用于简化DAO(Data Access Object)层开发的抽象基类。它属于`org.springframework.orm.hibernate5.support`包,为自定义DAO类提供了便捷的初始化和会话管理功能。...
4. **Spring的HibernateTemplate或HibernateDaoSupport**:Spring提供了HibernateTemplate和HibernateDaoSupport类,用于简化Hibernate操作。在这个Demo中,可能会使用它们来封装数据访问操作。 5. **Entity(实体...
总结,`HibernateDaoSupport`是Spring框架中为了简化Hibernate DAO层实现的一个重要工具,它通过提供SessionFactory注入、自动Session管理以及封装的HibernateTemplate,极大地提高了开发效率,降低了出错概率。...
在Java的持久化框架Hibernate中,查询操作是其核心功能之一。本篇文章将详细解析Hibernate中的几种主要查询方式,包括联表查询、分页查询、位置参数查询、占位符查询(冒号查询)以及统计查询,这些都是在实际开发中...
标题中的"Spring_2000_Spring_Hibernate_HibernateTemplate"显然指的是一个关于Spring框架、Hibernate ORM框架以及其在2000年左右的版本或应用方式的专题。在这个主题下,我们将深入探讨Spring框架的核心特性,以及...
而`HibernateTemplate`作为Spring框架对Hibernate的一种封装,更是进一步降低了开发难度,提高了开发效率。 #### 二、核心价值:专注业务逻辑 `HibernateTemplate`的主要价值在于让开发者能够将更多的精力聚焦于...
- Spring提供了`HibernateTemplate`和`HibernateDaoSupport`工具类,方便我们在Service层操作数据库。`HibernateTemplate`可以直接注入到Service类中,而`HibernateDaoSupport`需要我们继承,并在子类中注入`...
根据给定的信息,我们可以深入探讨Spring框架中与Hibernate集成的相关知识点,特别关注“HibernateDaoSupport”类及其在Spring环境中的应用。以下是对标题、描述以及部分文件内容的详细解析: ### 一、Spring与...
`hibernateDaoSupport`是Spring框架中提供的一种支持Hibernate操作的基类。它主要用于简化Hibernate与Spring集成过程中的编码工作,使得开发人员能够更加专注于业务逻辑的编写而非框架间的耦合问题。本文将详细介绍`...
2. **整合Hibernate和Spring**:Spring提供了HibernateTemplate和HibernateDaoSupport,用于简化Hibernate的使用。通过Spring的事务管理,可以在方法级别控制事务的开始、提交、回滚,避免了手动管理事务的复杂性。 ...