open session and Hibernate事务处理机制
它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。
Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。
OpenSessionInViewInterceptor配置
- <beans>
- <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/>
- </property>
- </bean>
- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="interceptors">
- <list>
- <ref bean="openSessionInViewInterceptor"/>
- </list>
- </property>
- <property name="mappings">
- ...
- </property>
- </bean> ... </beans>
<beans> <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor"/> </list> </property> <property name="mappings"> ... </property> </bean> ... </beans>
OpenSessionInViewFilter配置
- <web-app>
- ...
- <filter>
- <filter-name>hibernateFilter</filter-name>
- <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
- <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter> ... <filter-mapping>
- <filter-name>hibernateFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping> ... </web-app>
<web-app> ... <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView --> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> ... <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> ... </web-app>
很多人在使用OpenSessionInView过程中提及一个错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
看看OpenSessionInViewFilter里的几个方法
- protected void doFilterInternal(HttpServletRequest request,
- HttpServletResponse response,
- FilterChain filterChain) throws ServletException, IOException {
- SessionFactory sessionFactory = lookupSessionFactory();
- logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
- Session session = getSession(sessionFactory);
- TransactionSynchronizationManager.bindResource(
- sessionFactory, new SessionHolder(session));
- try {
- filterChain.doFilter(request, response);
- }
- finally {
- TransactionSynchronizationManager.unbindResource(sessionFactory);
- logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
- closeSession(session, sessionFactory);
- }
- }
- protected Session getSession(SessionFactory sessionFactory)
- throws DataAccessResourceFailureException {
- Session session = SessionFactoryUtils.getSession(sessionFactory, true);
- session.setFlushMode(FlushMode.NEVER);
- return session;
- }
- protected void closeSession(Session session,
- SessionFactory sessionFactory)throws CleanupFailureDataAccessException {
- SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);
- }
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { SessionFactory sessionFactory = lookupSessionFactory(); logger.debug("Opening Hibernate Session in OpenSessionInViewFilter"); Session session = getSession(sessionFactory); TransactionSynchronizationManager.bindResource( sessionFactory, new SessionHolder(session)); try { filterChain.doFilter(request, response); } finally { TransactionSynchronizationManager.unbindResource(sessionFactory); logger.debug("Closing Hibernate Session in OpenSessionInViewFilter"); closeSession(session, sessionFactory); } } protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { Session session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(FlushMode.NEVER); return session; } protected void closeSession(Session session, SessionFactory sessionFactory)throws CleanupFailureDataAccessException { SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory); }
关于绑定session的方式,通过看spring里TransactionSynchronizationManager的实现,发现:它维护一个 java.lang.ThreadLocal类型的resources,resources负责持有线程局部变量,这里resources持有的是一个 HashMap,通过TransactionSynchronizationManager.bindResource()方法在map里绑定和线程相关的所有变量到他们的标识上,包括如上所述的绑定在sessionFactory上的线程局部session。sessionHolder只不过是存放可以 hold一个session并可以和transtaction同步的容器。可以看到 OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该 sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。绑定以后,就可以防止每次不会新开一个Session呢?看看HibernateDaoSupport的情况:
- public final void setSessionFactory(SessionFactory sessionFactory) {
- this.hibernateTemplate = new HibernateTemplate(sessionFactory);
- }
- rotected final HibernateTemplate getHibernateTemplate() {
- return hibernateTemplate;
public final void setSessionFactory(SessionFactory sessionFactory) { this.hibernateTemplate = new HibernateTemplate(sessionFactory); } protected final HibernateTemplate getHibernateTemplate() { return hibernateTemplate; }
我们的DAO将使用这个template进行操作.
- public abstract class BaseHibernateObjectDao
- extends HibernateDaoSupportimplements BaseObjectDao {
- protected BaseEntityObject getByClassId(final long id) {
- BaseEntityObject obj =(BaseEntityObject)getHibernateTemplate().execute(new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException{
- return session.get(getPersistentClass(),new Long(id));
- }
- }
- );
- return obj;
- }
- public void save(BaseEntityObject entity) {
- getHibernateTemplate().saveOrUpdate(entity);
- }
- public void remove(BaseEntityObject entity) {
- try {
- getHibernateTemplate().delete(entity);
- } catch (Exception e) {
- throw new FlexEnterpriseDataAccessException(e);
- }
- }
- public void refresh(final BaseEntityObject entity) {
- getHibernateTemplate().execute(new HibernateCallback(){
- public Object doInHibernate(Session session)
- throws HibernateException {
- session.refresh(entity);
- return null;
- }
- }
- );
- }
- public void replicate(final Object entity) {
- getHibernateTemplate().execute(new HibernateCallback(){
- public Object doInHibernate(Session session)
- throws HibernateException{
- session.replicate(entity,ReplicationMode.OVERWRITE);
- eturn null;
- }
- });
- }
- }
public abstract class BaseHibernateObjectDao extends HibernateDaoSupportimplements BaseObjectDao { protected BaseEntityObject getByClassId(final long id) { BaseEntityObject obj =(BaseEntityObject)getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException{ return session.get(getPersistentClass(),new Long(id)); } } ); return obj; } public void save(BaseEntityObject entity) { getHibernateTemplate().saveOrUpdate(entity); } public void remove(BaseEntityObject entity) { try { getHibernateTemplate().delete(entity); } catch (Exception e) { throw new FlexEnterpriseDataAccessException(e); } } public void refresh(final BaseEntityObject entity) { getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException { session.refresh(entity); return null; } } ); } public void replicate(final Object entity) { getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException{ session.replicate(entity,ReplicationMode.OVERWRITE); eturn null; } }); } }
而HibernateTemplate试图每次在execute之前去获得Session,执行完就力争关闭Session
- public Object execute(HibernateCallback action) throws DataAccessException {
- Session session = (!this.allowCreate)SessionFactoryUtils.getSession(getSessionFactory(),
- false);
- SessionFactoryUtils.getSession(getSessionFactory(),
- getEntityInterceptor(),
- getJdbcExceptionTranslator()));
- boolean existingTransaction = TransactionSynchronizationManager.hasResource(
- getSessionFactory());
- if (!existingTransaction && getFlushMode() == FLUSH_NEVER) {
- session.setFlushMode(FlushMode.NEVER);
- }
- try {
- Object result = action.doInHibernate(session);
- flushIfNecessary(session, existingTransaction);
- return result;
- }
- catch (HibernateException ex) {
- throw convertHibernateAccessException(ex);
- }
- finally {
- SessionFactoryUtils.closeSessionIfNecessary(
- session, getSessionFactory());
- }
- }
public Object execute(HibernateCallback action) throws DataAccessException { Session session = (!this.allowCreate)SessionFactoryUtils.getSession(getSessionFactory(), false); SessionFactoryUtils.getSession(getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator())); boolean existingTransaction = TransactionSynchronizationManager.hasResource( getSessionFactory()); if (!existingTransaction && getFlushMode() == FLUSH_NEVER) { session.setFlushMode(FlushMode.NEVER); } try { Object result = action.doInHibernate(session); flushIfNecessary(session, existingTransaction); return result; } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } finally { SessionFactoryUtils.closeSessionIfNecessary( session, getSessionFactory()); } }
而这个SessionFactoryUtils能否得到当前的session以及closeSessionIfNecessary是否真正关闭 session,取决于这个session是否用sessionHolder和这个sessionFactory在我们最开始提到的 TransactionSynchronizationManager绑定。
- public static void closeSessionIfNecessary(Session session,
- SessionFactory sessionFactory)
- throws CleanupFailureDataAccessException {
- if (session == null || TransactionSynchronizationManager.hasResource(sessionFactory)) {
- return;
- }
- logger.debug("Closing Hibernate session");
- try {
- session.close();
- } catch (JDBCException ex) { // SQLException underneath
- throw new CleanupFailureDataAccessException("Could not close Hibernate session",
- ex.getSQLException());
- } catch (HibernateException ex) {
- throw new CleanupFailureDataAccessException("Could not close Hibernate session",
- ex);
- }
- }
public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory) throws CleanupFailureDataAccessException { if (session == null || TransactionSynchronizationManager.hasResource(sessionFactory)) { return; } logger.debug("Closing Hibernate session"); try { session.close(); } catch (JDBCException ex) { // SQLException underneath throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException()); } catch (HibernateException ex) { throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex); } }
在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为 Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。
可能的解決方式有:
1、将singleSession设为false,这样只要改web.xml,缺点是Hibernate Session的Instance可能会大增,使用的JDBC Connection量也会大增,如果Connection Pool的maxPoolSize设得太小,很容易就出问题。
2、在控制器中自行管理Session的FlushMode,麻烦的是每个有Modify的Method都要多几行程式。
session.setFlushMode(FlushMode.AUTO);
session.update(user);
session.flush();
3、Extend OpenSessionInViewFilter,Override protected Session getSession(SessionFactory sessionFactory),将FlushMode直接改为Auto。
4、让方法受Spring的事务控制。这就是常使用的方法: 采用spring的事务声明,使方法受transaction控制
- <bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- abstract="true">
- <property name="transactionManager" ref="transactionManager"/>
- <property name="proxyTargetClass" value="true"/>
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="save*">PROPAGATION_REQUIRED</prop>
- <prop key="add*">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="remove*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <bean id="userService" parent="baseTransaction">
- <property name="target">
- <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
- </property>
- </bean>
<bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"/> <property name="proxyTargetClass" value="true"/> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="userService" parent="baseTransaction"> <property name="target"> <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/> </property> </bean>
对于上例,则以save,add,update,remove开头的方法拥有可写的事务,如果当前有某个方法,如命名为importExcel(),则因没有transaction而没有写权限,这时若方法内有insert,update,delete操作的话,则需要手动设置flush model为Flush.AUTO,如 session.setFlushMode(FlushMode.AUTO); session.save(user); session.flush();
尽管Open Session In View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代码,这个方法实际上是被父类的doFilter调用的,因此,我们可以大约了解的OpenSessionInViewFilter调用流程: request(请求)->open session并开始transaction->controller->View(Jsp)->结束transaction并 close session.
一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这期间connection就一直被占用而不释放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,另一方面可能是网速慢,服务器与用户间传输时间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。 Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用。 另外:这样会产生一点危险性,毕竟把数据库访问的环境放到了表现层。(:用VO)
Hibernate是对JDBC的轻量级对象封装,Hibernate本身是不具备Transaction处理功能的,Hibernate的 Transaction实际上是底层的JDBC Transaction的封装,或者是JTA Transaction的封装,下面我们详细的分析:
Hibernate可以配置为JDBCTransaction或者是JTATransaction,这取决于你在hibernate.properties中的配置:
引用
#hibernate.transaction.factory_classnet.sf.hibernate.transaction.JTATransactionFactory #hibernate.transaction.factory_classnet.sf.hibernate.transaction.JDBCTransactionFactory
如果你什么都不配置,默认情况下使用JDBCTransaction,如果你配置为:
引用
hibernate.transaction.factory_classnet.sf.hibernate.transaction.JTATransactionFactory
将使用JTATransaction,不管你准备让Hibernate使用JDBCTransaction,还是JTATransaction,我的忠告就是什么都不配,将让它保持默认状态,如下:
引用
#hibernate.transaction.factory_classnet.sf.hibernate.transaction.JTATransactionFactory #hibernate.transaction.factory_classnet.sf.hibernate.transaction.JDBCTransactionFactory
在下面的分析中我会给出原因。
一、JDBC Transaction
看看使用JDBC Transaction的时候我们的代码例子:
- Session session = sf.openSession();
- Transaction tx = session.beginTransactioin();
- ... session.flush();
- tx.commit();
- session.close();
Session session = sf.openSession(); Transaction tx = session.beginTransactioin(); ... session.flush(); tx.commit(); session.close();
这是默认的情况,当你在代码中使用Hibernate的Transaction的时候实际上就是JDBCTransaction。那么JDBCTransaction究竟是什么东西呢?来看看源代码就清楚了: Hibernate2.0.3源代码中的类net.sf.hibernate.transaction.JDBCTransaction:
- public void begin() throws HibernateException {
- ...
- if (toggleAutoCommit) session.connection().setAutoCommit(false);
- ...
- }
public void begin() throws HibernateException { ... if (toggleAutoCommit) session.connection().setAutoCommit(false); ... }
这是启动Transaction的方法,看到 connection().setAutoCommit(false) 了吗?是不是很熟悉?
再来看
- public void commit() throws HibernateException {
- ...
- try {
- if (
- session.getFlushMode()!=FlushMode.NEVER )
- session.flush();
- try {
- session.connection().commit();
- committed = true;
- }
- ...
- toggleAutoCommit();
- }
public void commit() throws HibernateException { ... try { if ( session.getFlushMode()!=FlushMode.NEVER ) session.flush(); try { session.connection().commit(); committed = true; } ... toggleAutoCommit(); }
这是提交方法,看到connection().commit() 了吗?下面就不用我多说了,这个类代码非常简单易懂,通过阅读使我们明白Hibernate的Transaction都在干了些什么?我现在把用 Hibernate写的例子翻译成JDBC,大家就一目了然了:
- Connection conn = ...;
- <--- session = sf.openSession();
- conn.setAutoCommit(false);
- <--- tx = session.beginTransactioin();
- ...
- <--- ... conn.commit();
- <--- tx.commit();
- (对应左边的两句) conn.setAutoCommit(true);
- conn.close();
- <--- session.close();
Connection conn = ...; <--- session = sf.openSession(); conn.setAutoCommit(false); <--- tx = session.beginTransactioin(); ... <--- ... conn.commit(); <--- tx.commit(); (对应左边的两句) conn.setAutoCommit(true); conn.close(); <--- session.close();
看明白了吧,Hibernate的JDBCTransaction根本就是conn.commit而已,根本毫无神秘可言,只不过在Hibernate 中,Session打开的时候,就会自动conn.setAutoCommit(false),不像一般的JDBC,默认都是true,所以你最后不写 commit也没有关系,由于Hibernate已经把AutoCommit给关掉了,所以用Hibernate的时候,你在程序中不写 Transaction的话,数据库根本就没有反应。
二、JTATransaction
如果你在EJB中使用Hibernate,或者准备用JTA来管理跨Session的长事务,那么就需要使用JTATransaction,先看一个例子:
- javax.transaction.UserTransaction tx = new InitialContext().lookup("javax.transaction.UserTransaction");
- Session s1 = sf.openSession();
- ...
- s1.flush();
- s1.close();
- ...
- Session s2 = sf.openSession();
- ...
- s2.flush();
- s2.close();
- tx.commit();
javax.transaction.UserTransaction tx = new InitialContext().lookup("javax.transaction.UserTransaction"); Session s1 = sf.openSession(); ... s1.flush(); s1.close(); ... Session s2 = sf.openSession(); ... s2.flush(); s2.close(); tx.commit();
这是标准的使用JTA的代码片断,Transaction是跨Session的,它的生命周期比Session要长。如果你在EJB中使用 Hibernate,那么是最简单不过的了,你什么Transaction代码统统都不要写了,直接在EJB的部署描述符上配置某某方法是否使用事务就可以了。 现在我们来分析一下JTATransaction的源代码,
net.sf.hibernate.transaction.JTATransaction:
- public void begin(InitialContext context,
- ...
- ...
- ut = (UserTransaction) context.lookup(utName);
- ...
public void begin(InitialContext context, ... ... ut = (UserTransaction) context.lookup(utName); ...
看清楚了吗? 和我上面写的代码 “tx = new Initial Context?().lookup("javax.transaction.UserTransaction"); ”是不是完全一样?
- public void commit()
- ...
- ...
- if (newTransaction)
- ut.commit();
- ...
public void commit() ... ... if (newTransaction) ut.commit(); ...
JTATransaction的控制稍微复杂,不过仍然可以很清楚的看出来Hibernate是如何封装JTA的Transaction代码的。 但是你现在是否看到了什么问题? 仔细想一下,Hibernate Transaction是从Session中获得的,tx = session.beginTransaction(),最后要先提交tx,然后再session.close,这完全符合JDBC的 Transaction的操作顺序,但是这个顺序是和JTA的Transactioin操作顺序彻底矛盾的!!! JTA是先启动Transaction,然后启动Session,关闭Session,最后提交Transaction,因此当你使用JTA的 Transaction的时候,那么就千万不要使用Hibernate的Transaction,而是应该像我上面的JTA的代码片断那样使用才行。
总结:
1、在JDBC上使用Hibernate 必须写上Hibernate Transaction代码,否则数据库没有反应。此时Hibernate的Transaction就是Connection.commit而已;
2、在JTA上使用Hibernate 写JTA的Transaction代码,不要写Hibernate的Transaction代码,否则程序会报错;
3、在EJB上使用Hibernate 什么Transactioin代码都不要写,在EJB的部署描述符里面配置
|---CMT(Container Managed Transaction) |
|---BMT(Bean Managed Transaction) |
|----JDBC Transaction |
|----JTA Transaction
关于session:
1. servlet的session机制基于cookies,关闭浏览器的cookies则session失效即不能用网站的登录功能。
2. Hibernate Session.
1>. session 清理缓存时,按照以下顺序执行SQL语句:
session.save()的实体insert
实体的update
对集合的delete
集合元素的delete,update,insert
集合的insert
session.delete()的先后,执行实体的delete
2>. 默认时,session在以下时间点清理缓存: net.sf.hibernate.Transaction.commit():先清理缓存,再向数据库提交事务Session.find()或iterate()时,若缓存中持久化对象的属性发生了变化,就会先清缓存,以保证查询结果正确
3>. Session的commit()和flush()的区别:
flush()只执行SQL语句,不提交事务;commit()先调用flush(),再提交事务
4>. Session.setFlushMode()用于设定清理缓存的时间点:
清理缓存的模式 Session的查询方法 Session.commit() Session.flush() FlushMode.AUTO 清理清理清理 FlushMode.COMMIT 不清理清理清理 FlushMode.NEVER 不清理不清理清