OpenSessionInViewFilter

OpenSessionInViewFilter

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

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创建的会话,从而导致了"Already value [org.springframework.orm.hibernate4.SessionHolder@xxxxxxxx] for key [org.hibernate.internal.SessionFactoryImpl@xxxxxxxx] bound to thread [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(更多内容请参见下文)。然后,我不得不将视图(在某些情况下为控制器)中的位置的URL后缀更改为从使用“ .htm”到使用“ .flow”的Webflow。
因此,例如,如果我以前使用过“ /customer/customer-add.htm”的webflow网址,则将其更改为“ /customer/customer-add.flow”。

下一部分是我添加了一个从Spring的OpenSessionInViewFilter派生的类,该类的目的是将所有* .htm URL传递到OpenSessionInViewFilter,同时将所有* .flow URL都转发到链中的下一个过滤器(从而允许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>

09-05 05:57