Go Opensessioninviewfilter of Spring Configuration

Source: Internet
Author: User

Reference:
Opensessioninviewfilter function and configuration: http://www.yybean.com/opensessioninviewfilter-role-and-configuration
http://blog.csdn.net/fooe84/article/details/680449

Mainly related to classes:
Org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
Org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor

First, the role
The function of the open Session in view is to allow the same hibernate Session to be used for each request, so that the loading data can be lazy at any time of the request.
In the case of Singlesession=false, the same hibernate session will not be used throughout the request, but each data access will produce its own seesion, which equals no open session in View.
Opensessioninviewfilter does not flush the session by default, and flush mode is never

Spring's opensessioninviewfilter filter, primarily to implement Hibernate's lazy load function, is based on the principle that a hibernate session is requested.

Second, the configuration
It has two configurations, Opensessioninviewinterceptor and Opensessioninviewfilter (see Springside), with the same functionality, Just one in the Web. XML configuration, the other in Application.xml configuration only.

Open session in view keeps hibernate session open during request binding session to the current thread, so that the session can be used throughout the request. As in the view Layer PO can also be lazy loading data, such as ${company.employees}. When the view layer logic is complete, the session is automatically closed via the filter's Dofilter method or Interceptor's Posthandle method.
1,web.xml Configuration Opensessioninviewfilter
XML code
  1. <!--Hibernate Open Session in View filte-->
  2. <filter>
  3. <filter-name>hibernatefilter</filter-name>
  4. <filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</ filter-class>
  5. <!--singlesession default is True, if set to false it is useless opensessioninview --
  6. <init-param>
  7. <param-name>singlesession</param-name>
  8. <param-value>true</param-value>
  9. </init-param>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>hibernatefilter</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>
<!--Hibernate Open Session in View filte--><filter><filter-name>hibernatefilter</filter-name ><filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class ><!--singlesession Default is True, if set to False is useless 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>/*</ Url-pattern></filter-mapping>

2,applicationcontext.xml Configuration Opensessioninviewinterceptor
XML code
    1. <bean id= "Opensessioninviewinterceptor" &NBSP;&NBSP;
    2.      class= " Org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor ">  
    3.     <property  name= "sessionfactory"   ref= "sessionfactory"  />   
    4. </ Bean>  
<bean id= "Opensessioninviewinterceptor" class= " Org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor "><property name=" Sessionfactory " ref= "Sessionfactory"/></bean>


Third, attention
Although the open Session in view looks good, there are many side effects. Looking back at the Opensessioninviewfilter Dofilterinternal method code above, this method is actually called by the Dofilter of the parent class, so We can learn about the Opensessioninviewfilter invocation process:

Request->open session and start Transaction->controller->view (JSP), end transaction and close session.

Everything seems to be right, especially if there is no problem at the time of the local development test, but imagine if a step in the process is blocked, then the connection is occupied and not released. The most likely to be blocked is to write JSP this step, on the one hand may be the page content is large, Response.Write time is long, on the other hand may be slow, the server and the user between the transmission time long. When a large number of such cases occur, there is insufficient connection pool connection, causing the page suspended animation phenomenon.

Open Session In view is a double-edged sword, on the public web content of large-volume website please use with caution

[Opensessioninviewfilter of]spring configuration

Related Article

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.