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.
Anonymous 2:49 pm on May 24, 2008 Permalink
Thanks.
Anonymous 2:49 pm on May 24, 2008 Permalink
thanks a lot! code is better than 1000 blog post!
Mike 2:50 pm on May 24, 2008 Permalink
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.
cookie 2:51 pm on May 24, 2008 Permalink
Hi Mike,
Kudos on the great explanations and examples.
I am a newbie to Spring (as well as Java
). 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
Anonymous 2:51 pm on May 24, 2008 Permalink
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
weggyboy 2:52 pm on May 24, 2008 Permalink
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!
delta one 2:53 pm on May 24, 2008 Permalink
Excellent example!
Dan Allen 3:21 pm on May 24, 2008 Permalink
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.
David Newcomb 12:28 pm on October 12, 2009 Permalink
Your xml for urlMapping is incomplete! No value is set for:
and the property is closed. What is it supposed to be?
David Newcomb 12:29 pm on October 12, 2009 Permalink
Your xml for urlMapping is incomplete! No value is set for:
property name=”mappings”
and the “property” is not closed. What is it supposed to be?
David Newcomb 1:51 pm on October 12, 2009 Permalink
Also urlMapping doesn’t work if you are using annotations.
If you are using annotations you should probably go for the OpenSessionInViewFilter otherwise there is a rats nest of other support classes you need. Just spent the day reading all about it and I have lost the will to live
Mike 9:25 am on October 13, 2009 Permalink
@David Newcomb – The post isn’t really about how to set mappings. But you’ll want something like
which maps a request URI to a controller. Here are some examples…
http://www.google.com/search?q=SimpleUrlHandlerMapping+example
Pieno 7:52 am on July 11, 2010 Permalink
In Spring 3 you can use the tag to use the OpenSessionInViewInterceptor. There is an example on my blog: http://www.pieno.be/content/2010/opensessioninviewinterceptor-example-spring-3-web-mvc