Detailed usage of OpenSessionInViewFilter, opensessioninview

Source: Internet
Author: User

Detailed usage of OpenSessionInViewFilter, opensessioninview
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) {try {Session session =... sessionFactory. getCurrentSeesion (); // get the session object tx = session. beginTransaction (); // starts the transaction chain. doFliter (request, response); // pass to the next tx. commit (); // commit the transaction} catch (Exception e) {// an Exception occurs, roll back the transaction }}Analysis: When the request arrives, it will be intercepted by the interceptor first. After the data is obtained and displayed on the V layer, it will return to the Filter and commit the transaction --> close the session.Ii. osiv solution of springOpenSessionInViewFilter [Full name: org. springframework. orm. hibernate3.support. openSessionInViewFilter] is a support class provided by Spring for Hibernate. It mainly means to open the Hibernate Session when initiating a page request and keep the Session until the request ends, it is implemented through a Filter. Because Hibernate introduces the Lazy Load feature, Hibernate will throw a LazyLoad Exception if you want to get the value of the associated object from the object in the Hibernate Session cycle through the getter method. To solve this problem, Spring introduces this Filter, which leads to a longer life cycle of the Hibernate Session. There are two ways to configure and implement OpenSessionInView: OpenSessionInViewInterceptor and OpenSessionInViewFilter. The functions are the same, but one is configured in web. xml and the other is configured in application. xml. The common filter is used here: <! -- Configure the Spring filter to solve the problem of lazy loading -->
<Filter>
<Filter-name> OpenSessionInViewFilter </filter-name>
<Filter-class> org. springframework. orm. hibernate3.support. OpenSessionInViewFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> OpenSessionInViewFilter </filter-name>
<Url-pattern> *. action </url-pattern>
</Filter-mapping>


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.