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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28929579/article/details/77720405

在整合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

猜你喜欢

转载自blog.csdn.net/qq_28929579/article/details/77720405