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

时间:2015-04-07 15:30:14

标签: 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会话,然后HibernateFlowExecutionListener尝试创建一个而不是使用OpenSessionInViewFilter创建的那个,从而导致{{1}错误。

关于我可以调整或进一步深入研究故障排除的任何想法?任何解决方案或解决方法?有人见过这个吗?谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

我的解决方案是将我的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的filter-mapping中添加了一个新的* .flow url-pattern,现在我有一个自定义类覆盖了Spring OpenSessionInViewFilter(下面有更多内容)。然后,我必须更改我的视图中的位置的URL后缀(以及少数情况下的控制器),我使用“.htm”调用webflow,使用“.flow”。
因此,例如,如果我以前使用过'/customer/customer-add.htm'的webflow网址,我将其更改为'/customer/customer-add.flow'。

下一条是,我加入从Spring的的OpenSessionInViewFilter,其目的是将所有的* .htm的URL传递到的OpenSessionInViewFilter而简单地转发派生的类的所有*流式细胞网址在链中的下一个过滤器(从而允许HibernateFlowExecutionListener到最终处理URL并正确管理会话):

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>