今天整合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 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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
<!-- Creates the registry of flow definitions for this application -->
<!-- 要使用注解必须添加此项 -->
<context:annotation-config />
<!-- 自动检测组件
-->
<context:component-scan base-package="com.spring.service.impl,com.spring.dao,com.spring.aop" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描实体包 -->
<property name="packagesToScan">
<value>com.spring.domain</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--
加上下面这句话会出错
Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
-->
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 设定transactionManager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--启动spring事务注解功能-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
正如配置文件中红色文字所示,在单独使用hibernate时,总是会加上这项配置,但spring与hibernate结合时,千万不能加上这句话,我猜测出错原因是事务管理器中的session和当前拿到的session不是同一个session,事务管理器中拿到的session开启了事务,但当前得到的session并没有开启事务,导致出错
分享到:
相关推荐
之前一直找原因,最后知道了解决方法,具体请下载源码查看。特别是用到throw的事务中
springboot启动报错:java.lang.NoClassDefFoundError: org/springframework/transaction/ReactiveTransactionManager 不添加org.springframework.boot:spring-boot-starter-data-jpa:2.2.5.RELEASE,只有org....
Could not roll back Hibernate transaction.doc
主要给大家介绍了关于mysql出现报错:Deadlock found when trying to get lock; try restarting transaction的解决方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看...
org.hibernate.TransactionException: JDBC rollback failed
分布式简易事务回滚框架:biz-center-base-transaction
SAP NOTE TransactionManager Index server is in the process of starting up and taking too long.
The book keeps its focus on Hibernate without wasting time on nonessential third-party tools, so you’ll be able to immediately start building transaction-based engines and applications. Experienced ...
当你安装caffe报错: DBTest/1.TestWrite fails: MDB_BAD_TXN: Transaction cannot recover - it must be aborted 请下载安装
今天学习mongoDB的时候,启动总是失败,搜了好久找到这个文档解决了问题,是引擎的问题,需要的朋友可以参考下
介绍Hibernate+Transaction+myeclipse6+事例的应用参考工程源码.
Maven坐标:javax.transaction:javax.transaction-api:1.2; 标签:javax、transaction、api、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化...
Maven坐标:javax.transaction:javax.transaction-api:1.2; 标签:javax、transaction、api、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 ...
可以对hibernate的 Session 和 Transaction 有更深入的认识 希望对一些概念比较模糊的人 有帮助
spring-hibernate-maven-transaction整合
MySQL XA、Java事务API、atomikos等等,都是基于该规范做的实现。
1. 解压文件得到:jta-1_0_1B-classes.zip 2. cmd进入zip文件所在目录 ...mvn install:install-file -Dfile=./jta-1_0_1B-classes.zip -DgroupId=javax.transaction -DartifactId=jta -Dversion=1.0.1B -Dpackaging=jar
~、设计 SM国密依赖库选型。 自研 ;第3方库测试评估 ... 链码调用报错 : Error sending transaction invoke: Got unexpected status: SERVICE_UNAVAILABLE。 ~、fabric-1.4.1-gm 国密版本改造
eclipse安装activiti报错,缺少jar包。具体报错如下: Cannot complete the install because one or more required items could not be ... requires 'org.eclipse.emf.transaction 1.4.0' but it could not be found