开发者

DWR/OpenSessionInView "Session is Closed!" in Java and Hibernate

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</pro开发者_运维问答p>
            </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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜