spring管理hibernate session的问题探究

我们再用spring管理hibernate的时候, 我们会继承HibernateDaoSupport 或者HibernateTemplate类.

我们不知道这两个类之间有什么关系. 也没有去关闭session. 让我很是心不安,他可没有关闭session呀.如果..真的是后果不堪设想.百度了好久, 谷歌了好多. 都没有一个像样的说法. 说spring中HibernateDaoSupport会自己关闭session.

眼见为实.于是乎决定查看spring源码一探究竟.

先打开HibernateDaoSupoprt看看.

Hibernatedaosuport.java代码  

  1. public abstract class HibernateDaoSupport extends DaoSupport {
  2. private HibernateTemplate hibernateTemplate;
  3. public final void setSessionFactory(SessionFactory sessionFactory) {
  4. this.hibernateTemplate = createHibernateTemplate(sessionFactory);
  5. }
  6. protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
  7. return new HibernateTemplate(sessionFactory);
  8. }
  9. public final SessionFactory getSessionFactory() {
  10. return (this.hibernateTemplate != null ? this.hibernateTemplate.getSessionFactory() : null);
  11. }
  12. public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  13. this.hibernateTemplate = hibernateTemplate;
  14. }
  15. public final HibernateTemplate getHibernateTemplate() {
  16. return hibernateTemplate;
  17. }
  18. protected final void checkDaoConfig() {
  19. if (this.hibernateTemplate == null) {
  20. throw new IllegalArgumentException("sessionFactory or hibernateTemplate is required");
  21. }
  22. }
  23. protected final Session getSession()
  24. throws DataAccessResourceFailureException, IllegalStateException {
  25. return getSession(this.hibernateTemplate.isAllowCreate());
  26. }
  27. protected final Session getSession(boolean allowCreate)
  28. throws DataAccessResourceFailureException, IllegalStateException {
  29. return (!allowCreate ?
  30. SessionFactoryUtils.getSession(getSessionFactory(), false) :
  31. SessionFactoryUtils.getSession(
  32. getSessionFactory(),
  33. this.hibernateTemplate.getEntityInterceptor(),
  34. this.hibernateTemplate.getJdbcExceptionTranslator()));
  35. }
  36. protected final DataAccessException convertHibernateAccessException(HibernateException ex) {
  37. return this.hibernateTemplate.convertHibernateAccessException(ex);
  38. }
  39. protected final void releaseSession(Session session) {
  40. SessionFactoryUtils.releaseSession(session, getSessionFactory());
  41. }

在这里我们会注意到一个private 对象. 那就是HibernateTemplate. 这里面也有一个HibernateTemplate的set. get.

哦: 原来如此.呵呵,很白痴的事.

比如说. BaseDao extends HibernateDaoSupport 我们会super.getHibernateTemplate.find(hql);

super.getHibernateTemplate.save(obj);

和BaseDao extends HibernateTemplate 中super.find(hql)和super.save(obj);是等效的.  原来没有思考害的我改了一个多小时. 汗..

下面我们来看看HibernateTemplate是怎么样来操作session的呢.

照样我们贴出源代码. 由于这个类代码较多. 我只贴出来几个代表性的属性和方法, 供大家参考.

public class HibernateTemplate extends HibernateAccessor implements HibernateOperations {

private boolean allowCreate = true;

private boolean alwaysUseNewSession = false;

private boolean exposeNativeSession = false;

private boolean checkWriteOperations = true;

private boolean cacheQueries = false;

private String queryCacheRegion;

private int fetchSize = 0;

private int maxResults = 0;

public void ......();

} 一系列的方法.

下面我们看看save()方法.

Java代码  

  1. public Serializable save(final Object entity) throws DataAccessException {
  2. return (Serializable) execute(new HibernateCallback() {
  3. public Object doInHibernate(Session session) throws HibernateException {
  4. checkWriteOperationAllowed(session);
  5. return session.save(entity);
  6. }
  7. }, true);
  8. }

我们再看看update(), merge(), find()等方法的源码.

Java代码  

  1. //update 方法
  2. public void update(Object entity) throws DataAccessException {
  3. update(entity, null);
  4. }
  5. public void update(final Object entity, final LockMode lockMode) throws DataAccessException {
  6. execute(new HibernateCallback() {
  7. public Object doInHibernate(Session session) throws HibernateException {
  8. checkWriteOperationAllowed(session);
  9. session.update(entity);
  10. if (lockMode != null) {
  11. session.lock(entity, lockMode);
  12. }
  13. return null;
  14. }
  15. }, true);
  16. }
  17. //merge()
  18. public Object merge(final Object entity) throws DataAccessException {
  19. return execute(new HibernateCallback() {
  20. public Object doInHibernate(Session session) throws HibernateException {
  21. checkWriteOperationAllowed(session);
  22. return session.merge(entity);
  23. }
  24. }, true);
  25. }
  26. //find()
  27. public List find(String queryString) throws DataAccessException {
  28. return find(queryString, (Object[]) null);
  29. }
  30. public List find(String queryString, Object value) throws DataAccessException {
  31. return find(queryString, new Object[] {value});
  32. }
  33. public List find(final String queryString, final Object[] values) throws DataAccessException {
  34. return (List) execute(new HibernateCallback() {
  35. public Object doInHibernate(Session session) throws HibernateException {
  36. Query queryObject = session.createQuery(queryString);
  37. prepareQuery(queryObject);
  38. if (values != null) {
  39. for (int i = 0; i < values.length; i++) {
  40. queryObject.setParameter(i, values[i]);
  41. }
  42. }
  43. return queryObject.list();
  44. }
  45. }, true);
  46. }

细心的朋友们可能发现了. 他们无一例外的都调用了一个叫做execute()的方法. 对了. 我们再看看execute的面目.到底他干了一件什么样的事情呢?]

Hibernatetemplate.java execute()方法代码  

  1. public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
  2. Session session = getSession();
  3. boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());
  4. if (existingTransaction) {
  5. logger.debug("Found thread-bound Session for HibernateTemplate");
  6. }
  7. FlushMode previousFlushMode = null;
  8. try {
  9. previousFlushMode = applyFlushMode(session, existingTransaction);
  10. enableFilters(session);
  11. Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));
  12. Object result = action.doInHibernate(sessionToExpose);
  13. flushIfNecessary(session, existingTransaction);
  14. return result;
  15. }
  16. catch (HibernateException ex) {
  17. throw convertHibernateAccessException(ex);
  18. }
  19. catch (SQLException ex) {
  20. throw convertJdbcAccessException(ex);
  21. }
  22. catch (RuntimeException ex) {
  23. // Callback code threw application exception...
  24. throw ex;
  25. }
  26. finally {
  27. if (existingTransaction) {
  28. logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
  29. disableFilters(session);
  30. if (previousFlushMode != null) {
  31. session.setFlushMode(previousFlushMode);
  32. }
  33. }
  34. else {
  35. SessionFactoryUtils.releaseSession(session, getSessionFactory());
  36. }
  37. }
  38. }

抛掉其他的不管. finally中我们可以看到. 如果existingTransaction 他会

logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
disableFilters(session);
if (previousFlushMode != null) {
session.setFlushMode(previousFlushMode);
}

他并没有立即关闭session.

否则
SessionFactoryUtils.releaseSession(session, getSessionFactory());

他释放掉了session . 真的close()了吗?

我们在看看sessionFactoryUtil.的releaseSession()

Java代码  

  1. public static void releaseSession(Session session, SessionFactory sessionFactory) {
  2. if (session == null) {
  3. return;
  4. }
  5. // Only close non-transactional Sessions.
  6. if (!isSessionTransactional(session, sessionFactory)) {
  7. closeSessionOrRegisterDeferredClose(session, sessionFactory);
  8. }
  9. }
  10. static void closeSessionOrRegisterDeferredClose(Session session, SessionFactory sessionFactory) {
  11. Map holderMap = (Map) deferredCloseHolder.get();
  12. if (holderMap != null && sessionFactory != null && holderMap.containsKey(sessionFactory)) {
  13. logger.debug("Registering Hibernate Session for deferred close");
  14. Set sessions = (Set) holderMap.get(sessionFactory);
  15. sessions.add(session);
  16. if (!session.isConnected()) {
  17. // We‘re running against Hibernate 3.1 RC1, where Hibernate will
  18. // automatically disconnect the Session after a transaction.
  19. // We‘ll reconnect it here, as the Session is likely gonna be
  20. // used for lazy loading during an "open session in view" pase.
  21. session.reconnect();
  22. }
  23. }
  24. else {
  25. doClose(session);
  26. }
  27. }
  28. private static void doClose(Session session) {
  29. if (session != null) {
  30. logger.debug("Closing Hibernate Session");
  31. try {
  32. session.close();
  33. }
  34. catch (HibernateException ex) {
  35. logger.error("Could not close Hibernate Session", ex);
  36. }
  37. catch (RuntimeException ex) {
  38. logger.error("Unexpected exception on closing Hibernate Session", ex);
  39. }
  40. }
  41. }
时间: 2024-08-02 06:59:19

spring管理hibernate session的问题探究的相关文章

Spring管理Hibernate

为什么要用Hibernate框架? 既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢? 首先我们来看一下Hibernate进行操作的步骤.比如添加用户的一个步骤. 看截图,可以看出,Hibernate操作的步骤如下: 1.      获得Configuration对象 2.      创建SessionFactory 3.      创建Session 4.      打开事务 5.      进行持久化操作.比如上面的添加用户操作 6. 

Spring管理 hibernate 事务配置的五种方式

Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类,一个接口一个实现: Java代码   package com.dao; public interface UserDao { public void getUser(); } 实现: Java代码   package com.dao.impl; import org.springframework

Spring 管理Hibernate事务之事务传播机制

public enum Propagation {    REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),    SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),    MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),    NOT_SUPPORTED(TransactionDefinition.PROPAGATION_

Spring对hibernate的事物管理

把Hibernate用到的数据源Datasource,Hibernate的SessionFactory实例,事务管理器HibernateTransactionManager,都交给Spring管理.一.事务的4个特性:   原子性:一个事务中所有对数据库的操作是一个不可分割的操作序列,要么全做,要么全部做.   一致性:数据不会因为事务的执行而遭到破坏.   隔离性:一个事务的执行,不受其他事务(进程)的干扰.既并发执行的个事务之间互不干扰.   持久性:一个事务一旦提交,它对数据库的改变将是永

spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常 这个错误,网上说的原因一大推,反正我这个出的问题 是因为 虽然我在 spring 里把事务都配置好了,结果运行就出现这个错误,看看配置都没什么问题,都是网上 案例 照 写编码的,还是错的,结果发现是因为 我

Spring对Hibernate的session生效期(事物提交管理)介绍

在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据操作,然后提交事务,关闭事务,这样做的原因是因为Hibernate默认的事务自动提交是false,它是需要人为的手动提交事务,假如你不想每次都手动提交事务的话,你可以在hibernate.cfg.xml我文件中把它设置为事务自动提交: <propertyname="hibernate.connection.autocommit">true</property> 当我们Spring

Spring与Hibernate整合,实现Hibernate事务管理

1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar                 [spring对hibernate的支持] spring-tx-3.2.5.RELEASE.jar                     [事务相关] 2.配置文件 Product.hbm.xml <?xml version="1.0" encoding=

Spring对Hibernate事务管理【转】

在谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据操作,然后提交事务,关闭事务,我们这样做的原因是因为Hibernate默认的事务自动提交是false,他是需要我们人为的手动提交事务,假如你不想每次都手动提交事务的话,你可以在hibernate.cfg.xml我文件中把它设置为事务自动提交: <property name="hibernate

Hibernate事务管理-HibernateTransactionManager-对hibernate session的管理

由于对SSH还停留在比较初级的应用水平上,今天在遇到一个疑惑时折腾了很久,具体问题是这样的, 有这么一个测试方法, 1 public static void test1() { 2 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml","daoContext.xml"); 3 MgrManager mgr = (MgrManager)ctx.getBe