72Miles

Recent News

Archives

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

About the Author

Mike Nereson has been a professional software developer since 2000. He thinks open source is rad, and is an active volunteer Fire Fighter.

8 Comments »

  1. Posted by Anonymous

    May 24, 2008 @ 2:49 pm

    Thanks.

  2. Posted by Anonymous

    May 24, 2008 @ 2:49 pm

    thanks a lot! code is better than 1000 blog post! :D

  3. 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.

  4. 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

  5. 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 :(

  6. 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!

  7. Posted by delta one

    May 24, 2008 @ 2:53 pm

    Excellent example!

  8. 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.

RSS feed for comments on this post · TrackBack URI

Leave a Comment

About

72 Miles Software - open source software, search engine optimization analytics, and software startup information. Software by design. Read More

Categories

Links

Sitemap