http://xinjiang.iteye.com/blog/1678175

今天整合spring和hibernate中報了一個意想不到的錯誤:java

 

Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transactionmysql

at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:341)spring

at $Proxy16.save(Unknown Source)sql

 

配置文件以下:apache

Xml代碼  收藏代碼session

  1. <?xml version="1.0" encoding="UTF-8"?>  app

  2.   

  3. <beans xmlns="http://www.springframework.org/schema/beans"  dom

  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  this

  5.        xmlns:context="http://www.springframework.org/schema/context"  url

  6.        xmlns:aop="http://www.springframework.org/schema/aop"  

  7.        xmlns:tx="http://www.springframework.org/schema/tx"  

  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans   

  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

  10.            http://www.springframework.org/schema/context  

  11.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  

  12.            http://www.springframework.org/schema/aop  

  13.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  

  14.            http://www.springframework.org/schema/tx  

  15.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >  

  16.     <!-- Creates the registry of flow definitions for this application -->  

  17.       

  18.     <!-- 要使用註解必須添加此項 -->  

  19.     <context:annotation-config />  

  20.       

  21.     <!-- 自動檢測組件 

  22.      -->  

  23.     <context:component-scan base-package="com.spring.service.impl,com.spring.dao,com.spring.aop" />  

  24.   

  25.       

  26.     <!-- 配置數據源 -->  

  27.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  

  28.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  

  29.         <property name="url" value="jdbc:mysql://localhost:3306/test" />  

  30.         <property name="username" value="root" />  

  31.         <property name="password" value="root" />  

  32.     </bean>  

  33.       

  34.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  

  35.         <property name="dataSource" ref="dataSource"></property>  

  36.                   

  37.         <!-- 掃描實體包 -->  

  38.         <property name="packagesToScan">  

  39.             <value>com.spring.domain</value>  

  40.         </property>  

  41.           

  42.         <property name="hibernateProperties">  

  43.             <props>  

  44.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  

  45.                 <prop key="hibernate.show_sql">true</prop>  

  46.                   

  47.                 <!--   

  48.                     加上下面這句話會出錯   

  49.                     Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction  

  50.                 -->  

  51.                 <prop key="hibernate.current_session_context_class">thread</prop>  

  52.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  

  53.             </props>  

  54.         </property>  

  55.     </bean>  

  56.       

  57.     <!-- 設定transactionManager -->  

  58.      <bean id="txManager"  

  59.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  

  60.         <property name="sessionFactory" ref="sessionFactory" />  

  61.      </bean>  

  62.   

  63.     <!--啓動spring事務註解功能-->  

  64.     <tx:annotation-driven transaction-manager="txManager"/>  

  65.       

  66.       

  67. </beans>  

 

 

正如配置文件中紅色文字所示,在單獨使用hibernate時,老是會加上這項配置,但spring與hibernate結合時,千萬不能加上這句 話,我猜想出錯緣由是事務管理器中的session和當前拿到的session不是同一個session,事務管理器中拿到的session開啓了事務, 但當前獲得的session並無開啓事務,致使出錯

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息