java - Spring 4.1 MVC + Hibernate 4.3 + Webflow 2.4 + OpenSessionInViewFilter = java.lang.IllegalStateException,已经绑定(bind)到线程的键值

标签 java spring hibernate spring-mvc spring-webflow

我正在将应用程序从这些组件版本升级到最新版本:

Spring 3.0.4          -> Spring 4.1.6
Hibernate 3.3.0       -> Hibernate 4.3.8
Spring Webflow 2.0.7  -> Spring Webflow 2.4.1
Spring Security 2.0.4 -> Spring Security 3.2.6

我目前非常困惑与OpenSessionInViewFilter和Spring Webflows相关的问题。与我的 webflow 相关的代码都没有被执行,问题发生在 webflow 和 Hibernate SessionHolder 初始化时。在这次 Spring/Hibernate 升级期间,我没有更改 webflow 配置,并且在生产环境中一切都运行良好近 6 年。我收到以下异常,网络上几乎没有可用的帮助。堆栈跟踪有一英里长,因此我包含了我认为重要的部分。

2015/04/06 18:39:31 ERROR exception_jsp Stack Trace - 
org.springframework.webflow.execution.FlowExecutionException: Exception thrown in state 'null' of flow 'process/order'
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.wrap(FlowExecutionImpl.java:573)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.start(FlowExecutionImpl.java:227)
    at org.springframework.webflow.executor.FlowExecutorImpl.launchExecution(FlowExecutorImpl.java:140)
    at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:238)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    ...
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
    at 
...
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    at 
...
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Already value [org.springframework.orm.hibernate4.SessionHolder@30b921ac] for key [org.hibernate.internal.SessionFactoryImpl@486fe7cb] bound to thread [http-nio-8080-exec-8]
    at org.springframework.transaction.support.TransactionSynchronizationManager.bindResource(TransactionSynchronizationManager.java:190)
    at org.springframework.webflow.persistence.HibernateFlowExecutionListener.bind(HibernateFlowExecutionListener.java:250)
    at org.springframework.webflow.persistence.HibernateFlowExecutionListener.sessionStarting(HibernateFlowExecutionListener.java:137)
    at org.springframework.webflow.engine.impl.FlowExecutionListeners.fireSessionStarting(FlowExecutionListeners.java:117)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.start(FlowExecutionImpl.java:367)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.start(FlowExecutionImpl.java:223)
    ... 76 more

web.xml的相关部分:

  <filter> 
    <filter-name>openSessionInViewFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param><param-name>singleSession</param-name><param-value>false</param-value></init-param>
  </filter> 

  <filter-mapping> 
     <filter-name>openSessionInViewFilter</filter-name> 
     <url-pattern>*.htm</url-pattern> 
  </filter-mapping>

应用程序上下文配置的相关部分:

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
  <property name="order" value="0"/>
  <property name="flowRegistry" ref="flowRegistry"/>
</bean>
<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
  <property name="flowExecutor" ref="flowExecutor"/>
</bean>
...
<bean id="flowExecutionListener" class="org.springframework.webflow.persistence.HibernateFlowExecutionListener">
  <constructor-arg ref="sessionFactory" />
  <constructor-arg ref="transactionManager" />
</bean>
...
<webflow:flow-executor id="flowExecutor">
  <webflow:flow-execution-listeners>
    <webflow:listener ref="flowExecutionListener" />
    <webflow:listener ref="securityFlowExecutionListener" />
  </webflow:flow-execution-listeners>
</webflow:flow-executor>
...
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF">
    <webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>

Webflow配置文件:

<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
      http://www.springframework.org/schema/webflow/spring-webflow-2.4.xsd">

<persistence-context/>

<on-start>
  <evaluate expression="orderFormFactory.createOrderForm(externalContext.sessionMap.inboxCriteria)" result="flowScope.orderForm"/>
</on-start>
...

似乎OpenSessionInViewFilter首先创建Hibernate session ,然后HibernateFlowExecutionListener尝试创建一个 session ,而不是使用OpenSessionInViewFilter<创建的 session 从而导致 “键 [org.hibernate.internal.SessionFactoryImpl@xxxxxxxx] 的值 [org.springframework.orm.hibernate4.SessionHolder@xxxxxxxx] 已绑定(bind)到线程 [xxxxxxxxxxxxxxx]” 错误.

关于我可以调整或进一步深入研究以进行故障排除的任何想法?有什么解决方案或解决方法吗?还有其他人看过这个吗?谢谢您的帮助!

最佳答案

我的解决方案是将我的 Web 流从 OpenSessionInViewFilter 处理中分离出来。在进行更改之前,应用程序中以 *.htm 结尾的所有 URL 均通过 OpenSessionInViewFilter 进行路由。这是更改前的 web.xml:

<filter> 
  <filter-name>openSessionInViewFilter</filter-name> 
  <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  <init-param><param-name>singleSession</param-name><param-value>false</param-value></init-param>
</filter> 

<filter-mapping> 
  <filter-name>openSessionInViewFilter</filter-name> 
  <url-pattern>*.htm</url-pattern> 
</filter-mapping>

现在我的 web.xml 具有以下配置:

<filter> 
  <filter-name>openSessionInViewFilter</filter-name> 
  <filter-class>org.somepackage.MyCustomOpenSessionInViewFilter</filter-class>
  <init-param><param-name>singleSession</param-name><param-value>false</param-value></init-param>
</filter> 

<filter-mapping> 
  <filter-name>openSessionInViewFilter</filter-name> 
  <url-pattern>*.htm</url-pattern> 
  <url-pattern>*.flow</url-pattern>
</filter-mapping>

请注意,我在 openSessionInViewFilter 的过滤器映射中添加了一个新的 *.flow url-pattern,现在我有一个重写 Spring OpenSessionInViewFilter 的自定义类(更多内容见下文)。然后,我必须更改 View (在某些情况下还包括 Controller )中调用 Webflow 的位置的 URL 后缀,从使用“.htm”更改为使用“.flow”。
因此,例如,如果我以前使用过“/customer/customer-add.htm”的 webflow url,我将其更改为“/customer/customer-add.flow”。

下一部分是我添加了一个从 Spring 的 OpenSessionInViewFilter 派生的类,其目的是将所有 *.htm URL 传递到 OpenSessionInViewFilter,同时简单地将所有 *.flow URL 转发到链中的下一个过滤器(从而允许 HibernateFlowExecutionListener最终处理 URL 并正确管理 session ):

package org.somepackage;

import javax.servlet.http.HttpServletRequest;

import org.springframework.orm.hibernate4.support.OpenSessionInViewFilter;

public class MyCustomOpenSessionInViewFilter extends OpenSessionInViewFilter {

  @Override
  public boolean shouldNotFilter( HttpServletRequest request ) {
      return request.getRequestURI().contains( ".flow" );
  }   
}

最后,请注意,您应该保留 HibernateFlowExecutionListener 已有的 Spring 配置——无需进行任何更改:

<bean id="flowExecutionListener" class="org.springframework.webflow.persistence.HibernateFlowExecutionListener">
  <constructor-arg ref="sessionFactory" />
  <constructor-arg ref="transactionManager" />
</bean>

关于java - Spring 4.1 MVC + Hibernate 4.3 + Webflow 2.4 + OpenSessionInViewFilter = java.lang.IllegalStateException,已经绑定(bind)到线程的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495336/

相关文章:

java - Maven 无法下载 jar 依赖项

java - indexOf 还是二进制搜索?

java - 如何在 Spring 中关闭 VelocityViewResolver 错误?

Spring Cloud客户端尝试在服务器端口8888上运行

java - 应用程序部署在unix环境下的JBoss中时如何实现Spring LDAP身份验证?

java - 如何从 List<Object> 最佳实践中计算总和 Object.get Item()?

java - 如何扫描包中的 Hibernate 实体而不是使用 hbm.xml?

java - Spring Boot - Web 应用程序无法停止 MVStore 编写器线程

java - 测试时为类提供了错误类型的 id

java - JPA 和一组枚举值