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

open session in view 研究

    博客分类:
  • SSH
阅读更多

在没有使用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. <beans>   
  3. <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">   
  4. <property name="sessionFactory">   
  5. <ref bean="sessionFactory"/>   
  6. </property>   
  7. </bean>   
  8. <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">   
  9. <property name="interceptors">   
  10. <list>   
  11. <ref bean="openSessionInViewInterceptor"/>   
  12. </list>   
  13. </property>   
  14. <property name="mappings">   
  15. ...   
  16. </property>   
  17. </bean> ... </beans>   


OpenSessionInViewFilter配置

Xml代码 
  1.    
  2. <web-app>   
  3. ...   
  4. <filter>   
  5. <filter-name>hibernateFilter</filter-name>   
  6. <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>   
  7. <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->   
  8. <init-param>   
  9. <param-name>singleSession</param-name>   
  10. <param-value>true</param-value>   
  11. </init-param>   
  12. </filter> ... <filter-mapping>   
  13. <filter-name>hibernateFilter</filter-name>   
  14. <url-pattern>*.do</url-pattern>   
  15. </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里的几个方法

Java代码 
  1.     
  2.   
  3. protected void doFilterInternal(HttpServletRequest request,   
  4.         HttpServletResponse response,  
  5.         FilterChain filterChain) throws ServletException, IOException {   
  6.         SessionFactory sessionFactory = lookupSessionFactory();   
  7.         logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");   
  8.         Session session = getSession(sessionFactory);   
  9.         TransactionSynchronizationManager.bindResource(    
  10.                 sessionFactory, new SessionHolder(session));   
  11.         try {    
  12.             filterChain.doFilter(request, response);   
  13.             }   
  14.         finally {   
  15.             TransactionSynchronizationManager.unbindResource(sessionFactory);   
  16.         logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");   
  17.         closeSession(session, sessionFactory);   
  18.         }  
  19. }   
  20. protected Session getSession(SessionFactory sessionFactory)  
  21.                    throws DataAccessResourceFailureException {   
  22.         Session session = SessionFactoryUtils.getSession(sessionFactory, true);   
  23.         session.setFlushMode(FlushMode.NEVER);   
  24.         return session;  
  25. }  
  26. protected void closeSession(Session session,   
  27.         SessionFactory sessionFactory)throws CleanupFailureDataAccessException {   
  28.     SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);  
  29. }  


     关于绑定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.            


     我们的DAO将使用这个template进行操作.

Java代码 
  1.      
  2. public abstract class BaseHibernateObjectDao   
  3.                 extends HibernateDaoSupportimplements BaseObjectDao {       
  4. protected BaseEntityObject getByClassId(final long id) {                  
  5. BaseEntityObject obj =(BaseEntityObject)getHibernateTemplate().execute(new HibernateCallback() {                          
  6. public Object doInHibernate(Session session)   
  7.          throws HibernateException{                                      
  8.  return session.get(getPersistentClass(),new Long(id));                  
  9.        }                  
  10.     }  
  11. );                  
  12. return obj;        
  13. }       
  14. public void save(BaseEntityObject entity) {                    
  15.        getHibernateTemplate().saveOrUpdate(entity);       
  16. }       
  17. public void remove(BaseEntityObject entity) {                
  18. try {                       
  19.        getHibernateTemplate().delete(entity);                
  20. catch (Exception e) {                        
  21.        throw new FlexEnterpriseDataAccessException(e);               
  22.        }        
  23. }         
  24. public void refresh(final BaseEntityObject entity) {                 
  25.        getHibernateTemplate().execute(new HibernateCallback(){                            
  26.             public Object doInHibernate(Session session)   
  27.            throws HibernateException   {                                  
  28.                  session.refresh(entity);                                        
  29.                  return color: #7f
    分享到:
    评论

相关推荐

    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