【问题标题】:No Hibernate Session bound to thread没有绑定到线程的休眠会话
【发布时间】:2010-09-14 02:49:00
【问题描述】:

我正在使用 Spring MVC 和 Hibernate。

我想使用 OpenSessionInViewFilter 来允许延迟加载在视图层中正常工作。

OpenSessionInViewFilter 需要一个根应用程序上下文,因此我添加了一个 ContextLoaderListener 并将我的非视图相关配置文件从 DispatcherServlet 移到它。

app-config.xml 配置文件包含数据源相关的 bean。

当我使用 ContextLoaderListener 而不是 DispatcherServlet 加载 app-config.xml 时,我收到错误消息

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/XXXX/app/jobs] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'

更新:通过向 mvc-config.xml 添加组件扫描来修复此错误

但现在我明白了

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

我的理解是 DispatcherServlet 从根上下文继承 bean,因此将定义从 servlet 移动到根上下文应该没有区别。

web.xml

<filter>
  <filter-name>UrlRewriteFilter</filter-name>
  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>UrlRewriteFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <filter>
  <filter-name>openSessionInViewFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  <init-param>
   <param-name>singleSession</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>flushMode</param-name>
   <param-value>AUTO</param-value>
  </init-param>
  <init-param>
   <param-name>sessionFactoryBeanName</param-name>
   <param-value>sessionFactory</param-value>
  </init-param>
 </filter>

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

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   /WEB-INF/spring/app-config.xml
   /WEB-INF/spring/other-config.xml
  </param-value>
 </context-param>

 <!-- Handles all requests into the application -->
 <servlet>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/spring/mvc-config.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Maps all /app requests to the DispatcherServlet for handling -->
 <servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/app/*</url-pattern>
 </servlet-mapping>

app-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <!--
  Scans within the base package of the application for @Components to
  configure as beans
 -->
 <context:component-scan base-package="com.mycompany.app" />

 <!-- SQL Server -->
 <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
  <property name="url" value="jdbc:jtds:sqlserver://x.x.x.x/XXX" />
  <property name="username" value="XXX" />
  <property name="password" value="XXX" />
 </bean>

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="datasource" />
  <property name="configLocation">
   <value>/WEB-INF/hibernate.cfg.xml</value>
  </property>
  <property name="configurationClass">
   <value>org.hibernate.cfg.AnnotationConfiguration</value>
  </property>
  <property name="hibernateProperties">
   <value>hibernate.dialect=org.hibernate.dialect.SQLServerDialect</value>
  </property>
 </bean>

 <tx:annotation-driven />

 <bean name="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

</beans>

mvc-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Configures support for @Controllers -->
<mvc:annotation-driven />

<context:component-scan base-package="com.mycompany.app" />

 <bean id="tilesConfigurer"
  class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
  <property name="definitions">
   <list>
    <value>/WEB-INF/defs/general.xml</value>
   </list>
  </property>
 </bean>

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.tiles2.TilesView" />
 </bean>

</beans>

【问题讨论】:

  • 更新:我在 mvc-config.xml 添加了一个 并修复了映射问题。
  • public int getAllCount() { return (Integer) this.sessionFactory.getCurrentSession() .createCriteria(Job.class) .setProjection(Projections.rowCount()).uniqueResult(); }
  • 调用它的服务方法标记为事务性
  • 虽然我不使用 OpenSessionInView 模式,但您的代码似乎是正确的。我建议你打开登录,看看你的服务方法是否注册为Transactional

标签: hibernate spring spring-mvc


【解决方案1】:

我在这里看到的一个可能的问题是openSessionInViewFilter 映射在UrlRewriteFilter 之后。 UrlRewriteFilter 执行RequestDispatcher.forward(),因此在它之后映射的所有过滤器都应该有&lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;

<filter-mapping> 
    <filter-name>openSessionInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher>
</filter-mapping> 

【讨论】:

  • 添加 FORWARD 有效。此外,切换 OpenSessionInViewFilter 和 UrlRewriteFilter 使其工作。谢谢!
【解决方案2】:

&lt;tx:annotation-driven /&gt; 仅涵盖定义它的本地上下文。在这种情况下,它将覆盖 app-config.xml 中的 bean,因为这是我在您的配置中看到它的唯一位置。

根据您的事务的定义位置(并且基于您的错误,我假设它们是在 mvc-config.xml 上下文中的 bean 中定义的),这可能会导致 @Transactional 类或方法未连接使用 Spring 的事务管理。

我猜你需要将&lt;tx:annotation-driven /&gt; 添加到你的 mvc-config.xml 中。

以下来自Spring Reference

&lt;tx:annotation-driven/&gt; 只寻找 @Transactional 同在 bean 上 定义它的应用程序上下文。 这意味着,如果你把 &lt;tx:annotation-driven/&gt; 在一个 WebApplicationContextDispatcherServlet,它只检查 @Transactional你的豆子 控制器,而不是您的服务。 See Section 15.2, “The DispatcherServlet” 了解更多 信息。

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2023-03-11
    • 2011-10-25
    • 2011-01-31
    相关资源
    最近更新 更多