`
footman265
  • 浏览: 114486 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

继续Open Session In View

    博客分类:
  • j2SE
阅读更多

    转自:http://www.iteye.com/topic/186068

    

    在没有使用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配置 

 

<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> 

   很多人在使用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);
}

 

    关于绑定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); 
 }  
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;               
             }                
      });      
   }
}           

 

 而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());     
    } 
}       

 

   而这个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); 
       } 
}     

  在这个过程中,若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> 

   对于上例,则以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();   

 

    这是默认的情况,当你在代码中使用Hibernate的Transaction的时候实际上就是JDBCTransaction。那么JDBCTransaction究竟是什么东西呢?来看看源代码就清楚了:   Hibernate2.0.3源代码中的类net.sf.hibernate.transaction.JDBCTransaction:    这是默认的情况,当你在代码中使用Hibernate的Transaction的时候实际上就是JDBCTransaction。那么JDBCTransaction究竟是什么东西呢?来看看源代码就清楚了:   Hibernate2.0.3源代码中的类net.sf.hibernate.transaction.JDBCTransaction: 

 

  
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(); 
}   

 

    这是提交方法,看到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();   

  看明白了吧,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(); 

 这是标准的使用JTA的代码片断,Transaction是跨Session的,它的生命周期比Session要长。如果你在EJB中使用 Hibernate,那么是最简单不过的了,你什么Transaction代码统统都不要写了,直接在EJB的部署描述符上配置某某方法是否使用事务就可以了。 现在我们来分析一下JTATransaction的源代码, 

 

net.sf.hibernate.transaction.JTATransaction: 

 

   
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();
 ... 

    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 不清理不清理清

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Open Session in View模式.PPT

    Open Session in View模式.PPT

    struts2+hibernate3 open session in view

    struts2+hibernate3 做的小项目 使用了struts2插件实现pen session in view

    Open_Session_In_View详解.doc

    在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛...

    使用Spring引起的错误

    使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决

    SSH整合 struts+hibernate+spring

    SSH整合概述 应用IoC进行整合 应用AOP进行整合 Open Session In View模式

    Struts Spring Hibernate 整合 OpenSessionInView 例子

    为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: 1.通过接口编程 2.通过spring注入dao到 action 3.通过 open session in view filter 支持 延迟加载...

    Android google io 2012 opensource已通过编译无错误

    View detailed session, code lab, and speaker information, including speaker bios, photos, and Google+ profiles +1 sessions right from the app Participate in public #io12 conversations on Google+ Guide...

    plug-In PHP- 100 Power Solutions

    Open Session; Close Session; Secure Session; Manage Cookie; Block User by Cookie; Create Google Chart; Curl Get Contents; Fetch Wiki Page; Fetch Flickr Stream; Get Yahoo! Answers; Search Yahoo!; Get ...

    JCreatorV4

    You can open multiple files in the Open dialogue box, by holding down the Shift or Ctrl key down as you select each file. You can press ESC to return from Full Screen mode to normal mode.

    spring-jpa-wicket-bootstrap:使用 Spring、JPA、Hibernate、Wicket 和 Bootstrap 的 J2EE Web 模板。 在 Tomcat 和 Postgres DB 上测试

    它演示了MvC 、 SoC 、 IoC 、 DAO 、 Service layer和Open Session in View模式以及其他 J2EE 最佳实践。 该模板可以轻松扩展为功能齐全的基于 Wicket 的 Web 应用程序。 用法 该模板使用 maven 并在 Tomcat7 上...

    UE(官方下载)

    In this tutorial, we'll cover some of the basics of Unicode-encoded text and Unicode files, and how to view and manipulate it in UltraEdit. Search and delete lines found UEStudio and UltraEdit provide...

    PLSQL Developer 8.0.3.1510 中文注册版下载

    * Describe Window now also shows the view comments in the header * Export Tables would change nls_date_format in single session mode and dual session mode * Auto Replace now supports Undo to continue...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    - fixed bug with URLs in Open Document Text and Open Document Spreadsheet exports - fixed format string in XLS OLE export - fixed format string in XLS BIFF8 export - fixed output of the check boxes ...

    servlet2.4doc

    Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. -------------------------------------------------------------------------------...

    Bloodshed Dev-C++

    * Multi-select files in project-view (when "double-click to open" is configured in Environment Settings) * Resource files are treated as ordinary files now * Updates in "Project Options/Files" code * ...

    MobaXterm 10.8最新professional版专业版

    更新的内容包含: Version 10.8 (2018-07-07) Bugfix: fixed the error message which occured when running the graphical package manager...引用地址:http://www.open-open.com/lib/view/open1437704662490.html

    BCGControlBarPro.v12.00

    It is stored in CBCGPGridCtrl::m_PrintParams::m_pageInfo member and in CPrintInfo::m_lpUserData member of the CPrintInfo object used while printing at the current print session. Added an option to ...

    Sublime Text Build 3124 x64 Setup.exe

    File encoding of open files is now stored in the session Build Systems may define a cancel command using the "cancel" key Syntax: Added clear_scopes directive, to give more control over the generated ...

    PLSQL.Developer v11.0.0.1762 主程序+ v10中文包+keygen

    Triggers can now be created in the context of a table or view Folders added for Primary, Unique and Foreign key constraints for views Search Bar enhancements A new Search in files option allows you ...

    CE中文版-启点CE过NP中文.exe

    Added a statusbar to the hexview in memoryview Pointerscan for value scans now add the results to the overflow queue Opening a file and changing bytes do not change them to the file anymore (you need ...

Global site tag (gtag.js) - Google Analytics