May 24, 2008 @ 2:48 pm
Spring: OpenSessionInViewInterceptor & OpenSessionInViewFilter Examples
This post on OpenSessionInViewInterceptor vs. OpenSessionInViewFilter gets a lot of views from developers who are searching “OpenSessionInViewFilter” and “OpenSessionInViewFilter”. I’d guess, a lot of them are looking for examples. I don’t want to disappoint anyone so here are example setups of both the filter and the interceptor.
OpenSessionInViewInterceptor
This goes into your action-servlet.xml.
<beans> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor"/> </list> </property> <property name="mappings"> </bean> <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> </beans>
OpenSessionInViewFilter
This goes into your web.xml.
<web-app> <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>*.html</url-pattern> </filter-mapping> </web-app>
If you are not sure if you need to use the filter or the interceptor, check our post that explains the “>difference between the OpenSessionInViewFilter and the OpenSessionInViewInteceptor. The comments on that page offer some good insight too.
Now that you've read what we think, what did you like about the post? Where do you think we went wrong? Join or start the discussion.
Filed under Hibernate, Software Development, Spring Permalink
8 Comments »
RSS feed for comments on this post · TrackBack URI
Posted by Anonymous
May 24, 2008 @ 2:49 pm
Thanks.
Posted by Anonymous
May 24, 2008 @ 2:49 pm
thanks a lot! code is better than 1000 blog post!
Posted by Mike
May 24, 2008 @ 2:50 pm
Too true. I hate going to software blogs and seeing nothing but paragraph after paragraph. I want to see code that I can copy and paste, that has comments in it so that I can follow it.
Glad I could help. Cheers.
Posted by cookie
May 24, 2008 @ 2:51 pm
Hi Mike,
Kudos on the great explanations and examples.
I am a newbie to Spring (as well as Java :P). I have a question regarding OpenSessionInViewInterceptor/ Filter.
I have a Spring app that only selective controllers requires open sessions. Can I selectively “hook-up” the interceptor to those controllers?
Thanks a lot.
YS
Posted by Anonymous
May 24, 2008 @ 2:51 pm
For those of you using the Persistence API, there is also an EntityManager version:
org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor
Thanks for your example, Chris
PS I would put an example - but seems the comments box does not allow it
Posted by weggyboy
May 24, 2008 @ 2:52 pm
Nice Post, but it only works for views, doesn´t it?
I was trying to change some entries after the proposed changes and found out, that the write operations are not allowed with the default session setting for the openViewInSession filter.
After searching for a solution I´ve found that:
import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;
import org.hibernate.*;
import org.springframework.dao.*;
public class SessionFilter extends OpenSessionInViewFilter {
/*
* The default mode is FlushMode.NEVER
*
* @see org.springframework.orm.hibernate.support.OpenSessionInViewFilter#getSession(net.sf.hibernate.SessionFactory)
*/
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
Session session = super.getSession(sessionFactory);
session.setFlushMode(FlushMode.COMMIT);
return session;
}
/**
* we do an explicit flush here just in case we do not have an automated flush
*/
protected void closeSession(Session session, SessionFactory factory) {
session.flush();
super.closeSession(session, factory);
}
}
Now it works as wished!
Posted by delta one
May 24, 2008 @ 2:53 pm
Excellent example!
Posted by Dan Allen
May 24, 2008 @ 3:21 pm
You should read Spring in Seam, Part 3 (http://www.javaworld.com/javaworld/jw-05-2008/jw-05-spring-seam3.html) and chapters 8 and 9 of Seam in Action to discover why this Spring filter/interceptor is essential a huge *hack*. The persistence manager (Hibernate Session/JPA EntityManager) was designed to be extended across a series of requests, not be relegated to a single request or worse service layer call. LazyInitializationExceptions happen because the persistence manager is scoped incorrectly.