博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
http://xinjiang.iteye.com/blog/1678175
阅读量:7172 次
发布时间:2019-06-29

本文共 2805 字,大约阅读时间需要 9 分钟。

hot3.png

今天整合spring和hibernate中报了一个意想不到的错误:

 

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

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

at $Proxy16.save(Unknown Source)

 

配置文件如下:

Xml代码  收藏代码

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

  2.   

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

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

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

  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并没有开启事务,导致出错

转载于:https://my.oschina.net/u/1442577/blog/402323

你可能感兴趣的文章
AIX系统开启ftp服务
查看>>
linux 上拷贝文件到windows 上 文件出现锁的文件
查看>>
Xamarin iOS教程之编辑界面编写代码
查看>>
Construct Binary Tree from Preorder and Inorder Traversal
查看>>
写得好 git 提交信息
查看>>
Linux下获取线程TID的方法
查看>>
Redis和Memcache的区别分析(转)
查看>>
网络请求 http get post 一
查看>>
《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)
查看>>
Google Chrome Plus——绿色便携多功能谷歌浏览器
查看>>
Instant Run
查看>>
史上最详细的iOS之事件的传递和响应机制
查看>>
Subsequence(两个单调队列)
查看>>
一行代码实现iOS序列化与反序列化
查看>>
Xamarin提示Build-tools版本过老
查看>>
一次Win10安装体验
查看>>
主流界面搭建原理(类似百思不得姐主界面)
查看>>
Java正则表达式的语法与示例
查看>>
Batch
查看>>
取消Eclipse SVN的自动链接方式
查看>>