解决No Hibernate Session bound to thread, and configuration does not allow creation of non-transaction

在整合Spring4.2/hibernate3/SpringMVC时出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here报错。
以下是配置文件:

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>hibernate_Test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
  </context-param>

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

  <servlet>
     <servlet-name>SpringMVC</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 配置SpringMVC下的配置文件位置及名称 -->
     <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
     <servlet-name>SpringMVC</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter> 
      <filter-name>encodingFilter</filter-name> 
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
      <init-param> 
          <param-name>encoding</param-name> 
          <param-value>UTF-8</param-value> 
      </init-param> 
  </filter> 
  <filter-mapping> 
      <filter-name>encodingFilter</filter-name> 
      <url-pattern>/*</url-pattern> 
  </filter-mapping>

</web-app>
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">  

    <context:annotation-config/>  

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

    <context:property-placeholder location="classpath:db.properties"/>  

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >  
        <property name="username" value="${jdbc.user}"></property>  
        <property name="password" value="${jdbc.password}"></property>  
        <property name="driverClassName" value="${jdbc.driverClass}"></property>  
        <property name="url" value="${jdbc.jdbcUrl}"></property>    
    </bean>  

    <!-- 配置hibernate相关信息 -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >  
        <property name="dataSource" ref="dataSource"></property>  

        <property name="packagesToScan" value="com.test.entity"></property>

        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>

            </props>  
        </property>  

        <property name="annotatedClasses">  
            <value>com.test.entity.User</value>  
        </property> 

    </bean> 


     <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>
  • springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>  
<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:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">  

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



        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/jsp/"></property>
            <property name = "suffix" value = ".jsp"></property>
             <property name="order" value="2" ></property>
        </bean>


        <!-- 解决静态资源响应 -->
        <mvc:default-servlet-handler/>  
        <mvc:annotation-driven/>  

</beans>

经过一番搜索和思考,发现解决办法如下:
方法一:
1. 首先要开启注解事务。
2. 在service层或者dao层添加@Transactional注解
3. 把 <tx:annotation-driven transaction-manager="transactionManager" />放在springmvc.xml中就好使了或者是在springmvc.xml中配置<context:component-scan base-package="com.test.controller"></context:component-scan>
参见http://blog.csdn.net/z69183787/article/details/37819831及其评论。
原理:不让springmvc管理事务,让spring管理事务.

方法二:
在web.xml中配置一个过滤器:

    <filter>
       <filter-name>SpringOpenSessionInViewFilter</filter-name>
       <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

(注意applicationContext.xml中的某个bean id必须为”sessionFactory” )
原理:不知道, ̄□ ̄|| ,但有篇博文好像说的有点道理,大家读懂了记得告诉我~
地址:http://blog.csdn.net/bluishglc/article/details/6283871

### 回答1: org.hibernate.exception: no Hibernate session bound to thread是一个Hibernate框架的异常。在使用Hibernate进行数据库操作时,每个线程在进行数据库操作之前都需要绑定一个Hibernate会话(session),以确保数据库操作的一致性和正确性。这个异常表示当前线程尚未绑定Hibernate会话。 造成这个异常的原因可能有以下几种: 1. 在进行数据库操作之前未正确配置Hibernate的会话管理器。在使用Hibernate之前,我们需要在配置文件中正确配置Hibernate的会话管理器,以确保每个线程都能正确地绑定Hibernate会话。 2. 在数据库操作之前未正确打开Hibernate会话。在进行数据库操作之前,我们需要通过Hibernate的会话管理器打开一个新的Hibernate会话,再进行数据库操作。如果在操作之前未正确打开会话,就会出现这个异常。 3. 在数据库操作之前未正确关闭Hibernate会话。在进行数据库操作之后,我们需要通过Hibernate的会话管理器关闭之前打开的Hibernate会话,以释放资源。如果在操作之后未正确关闭会话,就会出现这个异常。 解决这个异常的方法有以下几种: 1. 检查配置文件中的会话管理器配置是否正确,并确保每个线程在进行数据库操作之前都能正确绑定Hibernate会话。 2. 在进行数据库操作之前,先打开一个新的Hibernate会话,并在操作之后,正确关闭会话。 3. 检查代码中是否有意外的异常导致会话未被正确关闭。可以使用try-catch语句确保在出现异常时也能正确关闭会话。 综上所述,org.hibernate.exception: no Hibernate session bound to thread异常是因为当前线程未绑定Hibernate会话所导致的。通过正确配置会话管理器,并在进行数据库操作前打开会话并在操作后关闭会话,可以解决这个异常。 ### 回答2: org.hibernate.exception: no Hibernate session bound to thread 是一个Hibernate框架的异常。它通常发生在没有将Hibernate Session与当前线程绑定时。 在使用Hibernate框架时,我们通常会在每个请求处理开始时创建一个Hibernate Session,然后在请求处理结束后关闭该Session。这样可以确保每个请求都有一个独立的Session来操作数据库。 当我们在查询、更新或删除数据库记录时,需要通过Hibernate Session来执行这些操作。但是,当没有将Session与当前线程绑定时,就会抛出"no Hibernate session bound to thread"异常。 解决这个异常的方法是在每个请求处理开始时,通过开启一个事务,将Hibernate Session与当前线程绑定起来。可以使用以下代码实现Session与线程的绑定: try { Session session = sessionFactory.openSession(); // 创建Session Transaction tx = session.beginTransaction(); // 开启事务 // 执行数据库操作 session.saveOrUpdate(entity); tx.commit(); // 提交事务 session.close(); // 关闭Session } catch (Exception e) { if (tx != null) { tx.rollback(); // 回滚事务 } e.printStackTrace(); } 上述代码中的sessionFactory是HibernateSessionFactory对象,通过它来创建Session。在开启事务后,执行完数据库操作后,需要提交事务并关闭Session。 这样,就可以确保在每个请求处理期间都有一个Hibernate Session与当前线程绑定,从而避免"no Hibernate session bound to thread"异常的发生。 ### 回答3: 这个错误意味着在使用Hibernate时没有将Hibernate session绑定到当前线程上。Hibernate是一个用于持久化数据的框架,它维护了一个会话(session)来管理与数据库的交互。当在应用程序中使用Hibernate进行数据库操作时,需要确保在每个线程中都有一个Hibernate session与之关联。 出现这个错误的原因可能有以下几种情况: 1. 没有正确地配置Hibernatesession管理方式: 需要在应用程序的配置文件中设置合适的session管理方式,例如使用ThreadLocalSessionContext。 2. 在使用Hibernate进行数据库操作之前,没有手动地将session与当前线程绑定: 需要在每个线程开始进行数据库操作之前,手动地将session与当前线程绑定。 3. 在进行数据库操作之前,已经将session与当前线程解绑: 需要确保在数据库操作完成之前,不要解除session与当前线程的绑定。 解决这个问题的方法有以下几种: 1. 使用合适的session管理方式: 确认在应用程序的配置文件中设置了合适的session管理方式,例如使用ThreadLocalSessionContext。 2. 在每个线程开始进行数据库操作之前,手动地将session与当前线程绑定: 可以使用Hibernate的getCurrentSession()方法来获取一个session,并将其与当前线程绑定。 3. 确保在数据库操作完成之前,不要解除session与当前线程的绑定: 在进行数据库操作的过程中,尽量避免手动解除session与当前线程的绑定。 总而言之,当出现"org.hibernateexception : no hibernate session bound to thread"错误时,需要检查Hibernatesession管理方式是否正确,以及在每个线程开始进行数据库操作时是否正确地将session与当前线程绑定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值