1. 程式人生 > >解決No Hibernate Session bound to thread, and configuration does not allow creation of non-transaction

解決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