Hibernate open Session in view

Source: Internet
Author: User
Tags sessions

http://fuliang.iteye.com/blog/146965


When we use Hibernate's lazy load to optimize performance, an exception occurs whenever the session closes and then attempts to access an object that is not loaded.     It is often useful to access data within a transaction, but sometimes we need to force this data to be loaded, such as accessing these model objects in Web view. Enforcing this data at the business level is often not a good solution, because different views use the business method in a way that is typically not the same as the data, so that the business method may be bound to a specific controller.
Add a façade layer above the business layer to solve this problem, it also adds a layer of less-needed encapsulation, adding complexity, as the example in the POJO in action is designed (POJO in action feels like a good book), and you can look at the book in detail.
The open session in view mode is usually a relatively good solution. The transaction ends at the service layer, but the associated hibernate session remains open until the view generation completes. This frees the database locks and connections early, and the view can be loaded by lazy load.
Spring supports this out-of-the-way pattern, with org.springframework.orm.hibernate.support.OpenSessionInViewFilter (which can be used with any web-tier technology) or Opensessioninviewinterceptor (used with the spring's web MVC framework).
These two implementations of spring support two modes of operation: Single session mode and deferred shutdown mode
1. Single session mode:
A single hibernate session is used for the entire HTTP request by manipulating the transaction on the request-scoped session. By default, it is a single session, which is a more efficient version of open sessions in view. The hibernate session within the request scope is treated as a first level cache, and only one persistent object is loaded within the entire request. The main disadvantage is that all session-managed objects must be unique, so that the view attach an object from the HttpSession can result in hibernate of duplicate object exceptions.
2. Delay Shutdown mode:
Each transaction uses its own session as usual, but each of these sessions remains open after the transaction completes, and closes after the view is generated. This allows you to avoid duplicate object problems by using the new Hibernate session, where all sessions remain open after the transaction is completed, allowing lazy load on each of them. However, if a single persistence object involves multiple transactions, it can cause problems. Hibernate requires a persistent object to be associated with a hibernate session, rather than with multiple. In this case, you should use a single session mode.

Configure in Web.xml:


    <filter>  
      <filter-name>OpenSessionInView</filter-name>  
      <filter-class> Org.springframework.orm.hibernate.support.opensessioninvewfilter</filter-class>  
    <!--If you use a deferred shutdown method  
      <init-param>  
        <param-name>singleSession</param-name>  
        <param-value>false </param-value>  
      </init-param>  
    -->  
    </filter>  
      
    <filter-mapping>  
      <filter-name>OpenSessionInView</filter-name>  
      <url-pattern>*.do</url-pattern><! --Example of a URL for a. do-->  
    </filter-mapping>  

Configuration of the Spring Web MVC opensessioninviewinterceptor:


    <bean id= "Opensessioninview" class= "Org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor" >  
     <property name= "Sessionfactory" >  
       <ref bean= "Sessionfactory" >  
     </property>  
    <!--  
     <property name= "singlesession" >  
       <value>false</value>  
     </If using deferred shutdown mode property>  
    -->  
    </bean>  
      
    <bean id= "myurlmapping" class= " Org.springframeword.web.servlet.handler.SimpleUrlHandlerMapping ">  
      <property name=" Interceptors ">  
        <list>  
          <ref bean= "Opensessioninview" >  
        </list>  
      </property>  
      <property name= "UrlMap" >  
        <!--URL mappings-->   
     </property>  
    </bean>  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.