文档章节

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

猪刚烈
 猪刚烈
发布于 2014/10/12 11:47
字数 1733
阅读 25
收藏 0

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

<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不再和当前线程绑定。

[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打开即可,无需事务的支持。

[http-8080-4] 2011-03-28 15:25:59,367 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
[http-8080-4] 2011-03-28 15:25:59,367 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] Opening single Hibernate Session in OpenSessionInViewFilter
[http-8080-4] 2011-03-28 15:25:59,367 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 5330113164767232
[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
[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;
[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]
[http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.hibernate.transaction.JDBCTransaction] begin
[http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true
[http-8080-4] 2011-03-28 15:25:59,439 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit
[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]
[http-8080-4] 2011-03-28 15:26:02,440 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit
[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]
[http-8080-4] 2011-03-28 15:26:02,440 DEBUG [org.hibernate.transaction.JDBCTransaction] commit
[http-8080-4] 2011-03-28 15:26:02,442 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit
[http-8080-4] 2011-03-28 15:26:02,443 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection
[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
[http-8080-4] 2011-03-28 15:26:02,443 DEBUG [org.hibernate.impl.SessionImpl] disconnecting session
[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
[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;
[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]
[http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.hibernate.transaction.JDBCTransaction] begin
[http-8080-4] 2011-03-28 15:26:02,445 DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: true
[http-8080-4] 2011-03-28 15:26:02,446 DEBUG [org.hibernate.transaction.JDBCTransaction] disabling autocommit
[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]
[http-8080-4] 2011-03-28 15:26:05,447 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] Initiating transaction commit
[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]
[http-8080-4] 2011-03-28 15:26:05,447 DEBUG [org.hibernate.transaction.JDBCTransaction] commit
[http-8080-4] 2011-03-28 15:26:05,449 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit
[http-8080-4] 2011-03-28 15:26:05,450 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection
[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
[http-8080-4] 2011-03-28 15:26:05,450 DEBUG [org.hibernate.impl.SessionImpl] disconnecting session
[http-8080-4] 2011-03-28 15:26:08,680 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] Closing single Hibernate Session in OpenSessionInViewFilter

本文转载自:http://blog.csdn.net/bluishglc/article/details/6283871

共有 人打赏支持
猪刚烈
粉丝 22
博文 708
码字总数 110
作品 1
海淀
程序员
私信 提问
spring 的OpenSessionInViewFilter简介【转】

假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptor。session会在transaction结束后关闭。 如果你...

周禄康
2010/04/14
379
0
hibernate学习笔记-关闭session之后如何进行懒加载《二》

当在使用Hibernate做开发的时候出现org.hibernate.LazyInitializationException: could not initialize proxy - no Session 错误提示 病症:这是一个lazy使用后的Exception,使用迟时加载,在...

xiaml
2014/04/09
0
0
OpenSessionInViewFilter作用及配置

一、作用 Spring为我们解决Hibernate的Session的关闭与开启问题。 Hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。如...

liangyx
2012/10/23
0
0
hibernate 延迟加载的错误 failed to lazily initialize

这个问题一般出现在一对多的情况下,解决的方法有两种 1、设置lazy=false 如果是用annotation,则配置如下 Xml代码 @OneToMany( targetEntity = CourseAuthorizationItem.class, cascade = ...

长平狐
2013/01/06
63
0
Spring AOP 日志拦截器的事务管理

如果要在方法执行前或后或抛出异常后加上一个自己的拦截器,或者一个环绕拦截器,在拦截器中执行一些操作,比如执行一些数据库操作,记录一些信 息,这些操作通过调用一个服务类的方法来执行...

哲别0
05/18
0
0

没有更多内容

加载失败,请刷新页面

加载更多

JavaScript 继承使用解析

继承,通俗地说,之前你写过一些类,这些类中有一些是而你现在要写的类的功能的子集或者基本相同,那么你不用完全重新写一个新的类,你可以把之前写的类拿过来使用.这样的一种代码重用过程就叫做继...

前端攻城小牛
14分钟前
0
0
深入解析JavaScript 原型继承

JavaScript 原型继承,学习js面向对象的朋友可以看看。十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下。如有不足之处,欢迎批评指正。 Object.prototype JavaScript是...

前端攻城老湿
15分钟前
0
0
2018阿里云双12——年末钜惠,低至2折

活动链接地址:https://m.aliyun.com/act/team1212/?params=N.JlJCGqQNL4

城市之雾
16分钟前
0
0
VMware前路难测,多个厂家群雄逐鹿

导读 以VMware为例,虚拟机巨头公布了第二财季报告所示,它第二财季收入同比增长13%,达到了21.7亿美元,而且该公司收入和每股收益均超出预期。 在人们高谈Salesforce、亚马逊等新兴云计算厂...

问题终结者
17分钟前
0
0
Vuex的初探与实战小结

1.概述 每一个 Vuex 应用的核心就是 store(仓库)。“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。 Vuex 和单纯的全局对象有以下两点不同: 1.Vuex 的状态存储是响...

peakedness丶
28分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部