Spring 4.1 MVC+;Hibernate 4.3+;Webflow 2.4+;OpenSessionInViewFilter=java.lang.IllegalStateException,已为绑定到线程的键指定了值

Spring 4.1 MVC+;Hibernate 4.3+;Webflow 2.4+;OpenSessionInViewFilter=java.lang.IllegalStateException,已为绑定到线程的键指定了值,java,spring,hibernate,spring-mvc,spring-webflow,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和springwe

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

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
和springwebflows相关的问题。甚至没有执行与我的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>

openSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
singleSessionfalse
openSessionInViewFilter
*.htm
应用程序上下文的相关部分配置:

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

...
...
...
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会话,然后
HibernateFlowExecutionListener
试图创建一个会话,而不是使用由
OpenSessionInViewFilter
创建的会话,从而导致
”值已经存在[org.springframework.orm.hibernate4。SessionHolder@xxxxxxxx]对于键[org.hibernate.internal]。SessionFactoryImpl@xxxxxxxx]绑定到线程[XXXXXXXXXXXXX]“
错误


有没有关于我可以调整或深入研究哪些问题以解决疑难问题的想法?有没有解决方案或解决办法?还有其他人看到了吗?谢谢你的帮助!

我的解决方案是将我的Web流从OpenSessionInViewFilter处理中分离出来。在我做出更改之前,应用程序中以*.htm结尾的所有URL都经过了路由OpenSessionInViewFilter。这是更改之前的my 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>
最后,请注意,您应该让HibernateFlowExecutionListener的Spring配置保持不变,不需要做任何更改:

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


这是一个困难的问题。一次有太多的主要jar升级。我怀疑还有多少人遇到过这个问题。最好的办法是为包路径“org.springframework.webflow”将记录器设置为“debug”并尝试两次确定SessionHolder obj的设置位置。此外,在进行此类重大升级时要小心。如果您有maven,请检查依赖关系层次结构,确保没有JAR被“降级”出于兼容性原因,我会自动发布。谢谢。我同意这一点,并且我一直在解决一些问题,这是最棘手的问题。也就是说,我有一个相当简单的解决方案,它可以工作,我会在我更彻底地审查它后很快发布它。升级spring webflow和spring后,我面临一个问题。你能帮我解决吗看看这个问题。
<bean id="flowExecutionListener" class="org.springframework.webflow.persistence.HibernateFlowExecutionListener">
  <constructor-arg ref="sessionFactory" />
  <constructor-arg ref="transactionManager" />
</bean>