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>