Chinaunix首页 | 论坛 | 博客
  • 博客访问: 867822
  • 博文数量: 215
  • 博客积分: 10062
  • 博客等级: 上将
  • 技术积分: 2235
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-01 13:21
文章分类

全部博文(215)

文章存档

2012年(1)

2011年(24)

2009年(16)

2008年(91)

2007年(83)

我的朋友

分类:

2008-04-25 13:47:48

 open session and hibernate事务处理机制


    * Hibernate事务处理机制
    * Spring源代码解析(八):Spring驱动Hibernate的实现

在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在

应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception; Open Session In View

提供了一种简便的方法,较好地解决了lazy loading问题.
它有两种配置方式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配置
Xml代码 复制代码

   1.     
   2.    
   3.
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">   
   4.    
   5.    
   6.
   
   7.
   
   8.    
   9.    
  10.    
  11.    
  12.
   
  13.
   
  14.    
  15. ...   
  16.
   
  17.
...
   

 













...

...



OpenSessionInViewFilter配置
Xml代码 复制代码

   1.   
   2.   
   3. ...  
   4.   
   5. hibernateFilter  
   6. org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   
   7.   
   8. singleSession  
   9. true  
  10.
  
  11.
...   
  12. hibernateFilter  
  13. *.do  
  14.
...
  

 

...

hibernateFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

singleSession
true

...
hibernateFilter
*.do
...



很多人在使用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里的几个方法
Java代码 复制代码

   1.    
   2. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, 
   3. FilterChain filterChain) throws ServletException, IOException { SessionFactory sessionFactory = lookupSessionFactory

();  
   4. logger.debug("Opening Hibernate Session in OpenSessionInViewFilter"); Session session = getSession(sessionFactory); 

TransactionSynchronizationManager.bindResource(  sessionFactory, new SessionHolder(session));  
   5. try {  filterChain.doFilter(request, response); }  
   6. finally { TransactionSynchronizationManager.unbindResource(sessionFactory);  
   7. logger.debug("Closing Hibernate Session in OpenSessionInViewFilter"); closeSession(session, sessionFactory);  
   8. } 
   9. }  
  10. protected Session getSession(SessionFactory sessionFactory)throws DataAccessResourceFailureException {  
  11. Session session = SessionFactoryUtils.getSession(sessionFactory, true);  
  12. session.setFlushMode(FlushMode.NEVER);  
  13. return session; 
  14. } 
  15. 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的情况:
Java代码 复制代码

   1.   
   2. public final void setSessionFactory(SessionFactory sessionFactory) {  
   3. this.hibernateTemplate = new HibernateTemplate(sessionFactory);  
   4. }   
   5. rotected final HibernateTemplate getHibernateTemplate() { 
   6. return hibernateTemplate;   
   7.           

 
 public final void setSessionFactory(SessionFactory sessionFactory) {
 this.hibernateTemplate = new HibernateTemplate(sessionFactory);
 } 
protected final HibernateTemplate getHibernateTemplate() {
 return hibernateTemplate; 
}        


我们的DAO将使用这个template进行操作.
Java代码 复制代码

   1.     
   2. public abstract class BaseHibernateObjectDao extends HibernateDaoSupportimplements BaseObjectDao {      
   3. protected BaseEntityObject getByClassId(final long id) {                 
   4. BaseEntityObject obj =(BaseEntityObject) getHibernateTemplate().execute(new HibernateCallback() {                      

  
   5. public Object doInHibernate(Session session) throws HibernateException {                                     
   6. return session.get(getPersistentClass(),new Long(id));                 
   7. }                 
   8. });                 
   9. return obj;       
  10. }      
  11. public void save(BaseEntityObject entity) {                  getHibernateTemplate().saveOrUpdate(entity);      
  12. }      
  13. public void remove(BaseEntityObject entity) {               
  14. try {                      
  15. getHibernateTemplate().delete(entity);               
  16. } catch (Exception e) {                       
  17. throw new FlexEnterpriseDataAccessException(e);              
  18. }      
  19. }        
  20. public void refresh(final BaseEntityObject entity) {               getHibernateTemplate().execute(new

HibernateCallback() {                           
  21. public Object doInHibernate(Session session) throws HibernateException {                                     

session.refresh(entity);                                       
  22. return null;                           
  23. }                
  24. });       
  25. }       
  26. public void replicate(final Object entity) {                getHibernateTemplate().execute(new HibernateCallback() {   

                       
  27. public Object doInHibernate(Session session)throws HibernateException {                                       
  28. session.replicate(entity,ReplicationMode.OVERWRITE);                 eturn null;                
  29. }                 
  30. });       
  31. }}          

  
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
Java代码 复制代码

   1.    
   2. public Object execute(HibernateCallback action) throws DataAccessException {   
   3. 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);   
   4. }       
   5. try {           
   6. Object result = action.doInHibernate(session);           flushIfNecessary(session, existingTransaction);            
   7. return result;     
   8. }     
   9. catch (HibernateException ex) {           
  10. throw convertHibernateAccessException(ex);      
  11. }      
  12. finally {      
  13. SessionFactoryUtils.closeSessionIfNecessary(   
  14. session, getSessionFactory());      
  15. }  
  16. }        

 
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绑定。
Java代码 复制代码

   1.    
   2. public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory) throws

CleanupFailureDataAccessException {  
   3. if (session == null || TransactionSynchronizationManager.hasResource(sessionFactory)) {  
   4. return;  
   5. }  
   6. logger.debug("Closing Hibernate session");  
   7. try { session.close();  
   8. } catch (JDBCException ex) { // SQLException underneath 
   9. throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException());  
  10. } catch (HibernateException ex) { throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex);

 
  11. }  
  12. }      

 
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控制
Xml代码 复制代码

   1.     
   2.    3. abstract="true">          
   4.           
   5.           
   6.                                
   7. PROPAGATION_REQUIRED,readOnly                
key="find*">PROPAGATION_REQUIRED,readOnly
                 PROPAGATION_REQUIRED,readOnly      

          PROPAGATION_REQUIRED                  
   8. PROPAGATION_REQUIRED                  
   9. PROPAGATION_REQUIRED                  
  10. PROPAGATION_REQUIRED            
          
  11.
      
  12.
      
  13.           
  14.               
  15.         
      
  16.
  

  
abstract="true">        
        
        
                             
PROPAGATION_REQUIRED,readOnly                 PROPAGATION_REQUIRED,readOnly 

               PROPAGATION_REQUIRED,readOnly                
key="save*">PROPAGATION_REQUIRED
                
PROPAGATION_REQUIRED                
PROPAGATION_REQUIRED                
PROPAGATION_REQUIRED            
        
    
    
        
            
        
    



对于上例,则以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的时候我们的代码例子:
Java代码 复制代码

   1.    
   2. Session session = sf.openSession();  
   3. Transaction tx = session.beginTransactioin();  
   4. ... session.flush();  
   5. tx.commit();  
   6. 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:
Java代码 复制代码

   1.    
   2. public void begin() throws HibernateException {  
   3. ...  
   4. if (toggleAutoCommit) session.connection().setAutoCommit(false);  
   5. ...  
   6. }   

 
public void begin() throws HibernateException {
...
if (toggleAutoCommit) session.connection().setAutoCommit(false);
...
}  


这是启动Transaction的方法,看到 connection().setAutoCommit(false) 了吗?是不是很熟悉?   
再来看
Java代码 复制代码

   1.     
   2. public void commit() throws HibernateException {  
   3. ...  
   4. try {  
   5.  if (  
   6.  session.getFlushMode()!=FlushMode.NEVER )  
   7. session.flush();  
   8. try {  
   9. session.connection().commit();  
  10. committed = true;  
  11. }  
  12. ...  
  13. toggleAutoCommit();  
  14. }    

  
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,大家就一目了然了:
Java代码 复制代码

   1.     
   2. Connection conn = ...;                
   3. <--- session = sf.openSession();  
   4. conn.setAutoCommit(false);    
   5. <--- tx = session.beginTransactioin();  
   6. ...  
   7. <--- ... conn.commit();                            
   8. <--- tx.commit();  
   9. (对应左边的两句) conn.setAutoCommit(true);  
  10. conn.close();                               
  11. <--- 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,先看一个例子:
Java代码 复制代码

   1.    
   2. javax.transaction.UserTransaction tx = new InitialContext().lookup("javax.transaction.UserTransaction");  
   3. Session s1 = sf.openSession();  
   4. ...  
   5. s1.flush();  
   6. s1.close();  
   7. ...  
   8. Session s2 = sf.openSession();  
   9. ... 
  10. s2.flush();  
  11. s2.close(); 
  12. 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:
Java代码 复制代码

   1.     
   2. public void begin(InitialContext context,  
   3. ...  
   4. ...  
   5. ut = (UserTransaction) context.lookup(utName);  
   6. ...  

  
public void begin(InitialContext context,
...
...
ut = (UserTransaction) context.lookup(utName);
...


看清楚了吗? 和我上面写的代码 “tx = new Initial Context?().lookup("javax.transaction.UserTransaction"); ”是不是完全一样?
Java代码 复制代码

   1.    
   2. public void commit()  
   3. ...  
   4. ...  
   5. if (newTransaction)  
   6. ut.commit(); 
   7.  ...  

 
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 不清理不清理清 
阅读(2077) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~