0

他们说使用 ajax 在 hibernate 和 webapp 中关闭会话是 java 和 spring 的常见问题,所以我必须像这样在 web.xml 中设置 OpenSessionInViewInFilter

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

但即便如此,当我尝试使用 Hibernate Criteria api 时,我得到“会话已关闭”休眠异常,所以我尝试了另一种使用 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">

和拦截器

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

同样的“会话已关闭”问题。请在这里帮助我。我是 Java 新手,非常感谢您的帮助。

我目前在 maven 的 jetty 插件中运行,版本 6.1.10。

4

2 回答 2

0

确保不要跨不同的线程传递数据。如果您使用延迟加载,这尤其容易(检查您的休眠映射默认值)。

假设一个实体 Y 是延迟加载的并且是从实体 X 引用的(通过 Hibernate),并且您将 X 从一个线程传递到另一个线程并且在另一个线程中执行 x.getY(),那么您就有麻烦了。

于 2009-11-27T10:01:50.797 回答
0

找到了。我需要在我的服务类中声明@Transactional。

于 2009-11-27T10:21:07.673 回答