spring对session和事务的管理以及OpenSessionInViewFilter是如何工作

为了弄清楚spring对session和事务的管理以及OpenSessionInViewFilter是如何工作的,可监控以下类的日志:
  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter,
  org.springframework.orm.hibernate3.HibernateTransactionManager,
  org.hibernate.transaction.JDBCTransaction,
  org.hibernate.impl.SessionImpl
具体可参考下面的配置:

[xhtml] view plaincopyprint?

  1.    <appender name="sessionTransactionAppender" class="org.apache.log4j.DailyRollingFileAppender">  

  2.     <param name="File" value="session_transaction.log" />  

  3.     <param name="Append" value="false" />  

  4.     <param name="Threshold" value="DEBUG" />  

  5.     <param name="DatePattern" value="'.'yyyy-MM-dd" />  

  6.     <layout class="org.apache.log4j.PatternLayout">  

  7.         <param name="ConversionPattern" value="[%t] %d %-5p [%c] %m%n" />  

  8.     </layout>  

  9. </appender>  

  10. <category name="org.hibernate.impl.SessionImpl">  

  11.       <priority value="ALL" />  

  12.      <appender-ref ref="sessionTransactionAppender"/>  

  13. </category>  

  14.    <category name="org.springframework.orm.hibernate3.HibernateTransactionManager">  

  15.       <priority value="ALL" />  

  16.      <appender-ref ref="sessionTransactionAppender"/>  

  17. </category>  

  18. <category name="org.springframework.orm.hibernate3.support.OpenSessionInViewFilter">  

  19.       <priority value="debug" />  

  20.      <appender-ref ref="sessionTransactionAppender"/>  

  21. </category>  

<appender name="sessionTransactionAppender" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="session_transaction.log" /> <param name="Append" value="false" /> <param name="Threshold" value="DEBUG" /> <param name="DatePattern" value="'.'yyyy-MM-dd" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%t] %d %-5p [%c] %m%n" /> </layout> </appender> <category name="org.hibernate.impl.SessionImpl">    <priority value="ALL" />      <appender-ref ref="sessionTransactionAppender"/> </category>    <category name="org.springframework.orm.hibernate3.HibernateTransactionManager">    <priority value="ALL" />      <appender-ref ref="sessionTransactionAppender"/> </category> <category name="org.springframework.orm.hibernate3.support.OpenSessionInViewFilter">    <priority value="debug" />      <appender-ref ref="sessionTransactionAppender"/> </category>

下面是在一个不使用OpenSessionInViewFilter的项目中,一次请求(所有条目都是在一个线程里的)调用两次getAllForumGroups方法(此方法上声明了事务控制)打出的日志:
日志中可以清楚地看到:每次事务开启时会打开一个新的session,事务结束时会关闭该session,session不再和当前线程绑定。

[xhtml] view plaincopyprint?

  1. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,304 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330116498333696  

  2. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,698 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Creating new transaction with name [oobbs.application.service.forum.ForumGroupService.getAllForumGroups]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT  

  3. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,700 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330116500275200  

  4. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,701 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Opened new Session [org.hibernate.impl.SessionImpl@a0e220] for Hibernate transaction  

  5. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,712 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@a0e220]  

  6. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,737 DEBUG [org.hibernate.transaction.JDBCTransaction] begin  

  7. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,739 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true  

  8. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,740 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit  

  9. [7143355@qtp-21544349-0] 2011-03-28 15:39:33,746 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Exposing Hibernate transaction as JDBC transaction [org.apache.commons.dbcp.PoolableConnection@1ac9683]  

  10. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,205 DEBUG [org.hibernate.impl.SessionImpl] initializing proxy: [oobbs.domainmodel.user.User#-1]  

  11. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,419 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit  

  12. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,423 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@a0e220]  

  13. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,426 DEBUG [org.hibernate.transaction.JDBCTransaction] commit  

  14. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,447 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit  

  15. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,452 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection  

  16. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,458 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Closing Hibernate Session [org.hibernate.impl.SessionImpl@a0e220] after transaction  

  17. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,464 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Creating new transaction with name [oobbs.application.service.forum.ForumGroupService.getAllForumGroups]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT  

  18. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,465 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330116503408640  

  19. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,466 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Opened new Session [org.hibernate.impl.SessionImpl@f7d5a6] for Hibernate transaction  

  20. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,469 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@f7d5a6]  

  21. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,470 DEBUG [org.hibernate.transaction.JDBCTransaction] begin  

  22. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,474 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true  

  23. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,476 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit  

  24. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,478 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Exposing Hibernate transaction as JDBC transaction [org.apache.commons.dbcp.PoolableConnection@1ac9683]  

  25. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,652 DEBUG [org.hibernate.impl.SessionImpl] initializing proxy: [oobbs.domainmodel.user.User#-1]  

  26. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,795 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit  

  27. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,796 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@f7d5a6]  

  28. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,798 DEBUG [org.hibernate.transaction.JDBCTransaction] commit  

  29. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,804 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit  

  30. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,805 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection  

  31. [7143355@qtp-21544349-0] 2011-03-28 15:39:34,810 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Closing Hibernate Session [org.hibernate.impl.SessionImpl@f7d5a6] after transaction  

[7143355@qtp-21544349-0] 2011-03-28 15:39:33,304 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330116498333696 [7143355@qtp-21544349-0] 2011-03-28 15:39:33,698 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Creating new transaction with name [oobbs.application.service.forum.ForumGroupService.getAllForumGroups]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT [7143355@qtp-21544349-0] 2011-03-28 15:39:33,700 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330116500275200 [7143355@qtp-21544349-0] 2011-03-28 15:39:33,701 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Opened new Session [org.hibernate.impl.SessionImpl@a0e220] for Hibernate transaction [7143355@qtp-21544349-0] 2011-03-28 15:39:33,712 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@a0e220] [7143355@qtp-21544349-0] 2011-03-28 15:39:33,737 DEBUG [org.hibernate.transaction.JDBCTransaction] begin [7143355@qtp-21544349-0] 2011-03-28 15:39:33,739 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true [7143355@qtp-21544349-0] 2011-03-28 15:39:33,740 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit [7143355@qtp-21544349-0] 2011-03-28 15:39:33,746 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Exposing Hibernate transaction as JDBC transaction [org.apache.commons.dbcp.PoolableConnection@1ac9683] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,205 DEBUG [org.hibernate.impl.SessionImpl] initializing proxy: [oobbs.domainmodel.user.User#-1] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,419 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,423 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@a0e220] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,426 DEBUG [org.hibernate.transaction.JDBCTransaction] commit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,447 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,452 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection [7143355@qtp-21544349-0] 2011-03-28 15:39:34,458 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Closing Hibernate Session [org.hibernate.impl.SessionImpl@a0e220] after transaction [7143355@qtp-21544349-0] 2011-03-28 15:39:34,464 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Creating new transaction with name [oobbs.application.service.forum.ForumGroupService.getAllForumGroups]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT [7143355@qtp-21544349-0] 2011-03-28 15:39:34,465 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330116503408640 [7143355@qtp-21544349-0] 2011-03-28 15:39:34,466 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Opened new Session [org.hibernate.impl.SessionImpl@f7d5a6] for Hibernate transaction [7143355@qtp-21544349-0] 2011-03-28 15:39:34,469 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@f7d5a6] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,470 DEBUG [org.hibernate.transaction.JDBCTransaction] begin [7143355@qtp-21544349-0] 2011-03-28 15:39:34,474 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true [7143355@qtp-21544349-0] 2011-03-28 15:39:34,476 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,478 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Exposing Hibernate transaction as JDBC transaction [org.apache.commons.dbcp.PoolableConnection@1ac9683] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,652 DEBUG [org.hibernate.impl.SessionImpl] initializing proxy: [oobbs.domainmodel.user.User#-1] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,795 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,796 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@f7d5a6] [7143355@qtp-21544349-0] 2011-03-28 15:39:34,798 DEBUG [org.hibernate.transaction.JDBCTransaction] commit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,804 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit [7143355@qtp-21544349-0] 2011-03-28 15:39:34,805 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection [7143355@qtp-21544349-0] 2011-03-28 15:39:34,810 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Closing Hibernate Session [org.hibernate.impl.SessionImpl@f7d5a6] after transaction

  下面是在一个使用了OpenSessionInViewFilter的项目中,一次请求(所有条目都是在一个线程里的)调用两次getRole方法(此方法上声明了事务控制)打出的日志:
日志中可以清楚地看到:请求刚发起时就会创建一个session实例,并绑定到当前线程上,至请求结束时,关闭该线程。在请求期间所有事务都使用这一个session。
  另一方面我们还可以看到:数据库连接connection保持打开的时间正是事务从开始到提交的时间,为了测试,getRole方法中故意设定了3秒的延迟,从日志上看出数据库连接也基本上维持了3秒。这是很多系统出现无法获取数据库连接问题的原因。在一个使用了连接池的系统里,连接数有一个最大上限,如果某个请求的处理耗时过长,则当前数据库连接在短时间内就得不到释放。当前并发访问量急速上升时,连接池的连接用会迅速用光,导致后续的请求因无法得到数据库连接而失败。
  最后,至于为什么只需要保证session一直打开就能避免lazy loading时不报错,原因就在于,观察下面的log可以发现,每当在事务期间时autocommit就被禁用,事务一执行完毕就又恢复至可用。这样,在jsp页面上读取lazy对象时,并不是以事务方式进行的,而是以autocommit的方式提交select语句的。因此只需要保持session打开即可,无需事务的支持。

[xhtml] view plaincopyprint?

  1. [http-8080-4] 2011-03-28 15:25:59,367 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter  

  2. [http-8080-4] 2011-03-28 15:25:59,367 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] Opening single Hibernate Session in OpenSessionInViewFilter  

  3. [http-8080-4] 2011-03-28 15:25:59,367 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330113164767232  

  4. [http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Found thread-bound Session [org.hibernate.impl.SessionImpl@1d8f985] for Hibernate transaction  

  5. [http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Creating new transaction with name [sf.demo.RoleService.getRole]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT;  

  6. [http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@1d8f985]  

  7. [http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.hibernate.transaction.JDBCTransaction] begin  

  8. [http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true  

  9. [http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit  

  10. [http-8080-4] 2011-03-28 15:25:59,440 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Exposing Hibernate transaction as JDBC transaction [com.mchange.v2.c3p0.impl.NewProxyConnection@16f7142]  

  11. [http-8080-4] 2011-03-28 15:26:02,440 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit  

  12. [http-8080-4] 2011-03-28 15:26:02,440 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1d8f985]  

  13. [http-8080-4] 2011-03-28 15:26:02,440 DEBUG [org.hibernate.transaction.JDBCTransaction] commit  

  14. [http-8080-4] 2011-03-28 15:26:02,442 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit  

  15. [http-8080-4] 2011-03-28 15:26:02,443 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection  

  16. [http-8080-4] 2011-03-28 15:26:02,443 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Not closing pre-bound Hibernate Session [org.hibernate.impl.SessionImpl@1d8f985] after transaction  

  17. [http-8080-4] 2011-03-28 15:26:02,443 DEBUG [org.hibernate.impl.SessionImpl] disconnecting session  

  18. [http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Found thread-bound Session [org.hibernate.impl.SessionImpl@1d8f985] for Hibernate transaction  

  19. [http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Creating new transaction with name [sf.demo.RoleService.getRole]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT;  

  20. [http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@1d8f985]  

  21. [http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.hibernate.transaction.JDBCTransaction] begin  

  22. [http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true  

  23. [http-8080-4] 2011-03-28 15:26:02,446 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit  

  24. [http-8080-4] 2011-03-28 15:26:02,446 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Exposing Hibernate transaction as JDBC transaction [com.mchange.v2.c3p0.impl.NewProxyConnection@1fb8de1]  

  25. [http-8080-4] 2011-03-28 15:26:05,447 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit  

  26. [http-8080-4] 2011-03-28 15:26:05,447 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1d8f985]  

  27. [http-8080-4] 2011-03-28 15:26:05,447 DEBUG [org.hibernate.transaction.JDBCTransaction] commit  

  28. [http-8080-4] 2011-03-28 15:26:05,449 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit  

  29. [http-8080-4] 2011-03-28 15:26:05,450 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection  

  30. [http-8080-4] 2011-03-28 15:26:05,450 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Not closing pre-bound Hibernate Session [org.hibernate.impl.SessionImpl@1d8f985] after transaction  

  31. [http-8080-4] 2011-03-28 15:26:05,450 DEBUG [org.hibernate.impl.SessionImpl] disconnecting session  

  32. [http-8080-4] 2011-03-28 15:26:08,680 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] Closing single Hibernate Session in OpenSessionInViewFilter  

你可能感兴趣的:(spring对session和事务的管理以及OpenSessionInViewFilter是如何工作)