Question

They say that a closed session in hibernate and webapp with ajax is a common problem with java and spring so I have to set the OpenSessionInViewInFilter in the web.xml like this

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>    
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
</context-param>
<filter>
    <filter-name>springFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>springFilter</filter-name>
    <url-pattern>/dwr/*</url-pattern>
</filter-mapping>

But even with that, I get the "Session is Closed" hibernate exception when I try to use Hibernate Criteria api, so I tried another spring way using OpenSessioninViewInterceptor

<bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="alwaysUseFullPath" value="false"/>
     <property name="mappings">
            <props>
                <prop key="*">dwrController</prop>
            </props>
    </property>
    <property name="interceptors">

and the interceptor

<bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">

Same "Session is closed" problem. Please help me here. I am new in Java so your help is very much appreciated.

I am currently running inside maven's jetty plugin, version 6.1.10.

Was it helpful?

Solution

Make sure you don't pass the data across different threads. This is especially easy if you use lazy loading (check your hibernate mapping defaults).

Say if an entity Y is lazy loaded and is referenced from entity X (via Hibernate) and you pass X from one thread to another and in this another thread you do x.getY(), you are in trouble.

OTHER TIPS

Found it. I need to declare @Transactional in my service class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top