如果你已经排除了以下问题:
https://blog.csdn.net/qq_20597727/article/details/84900994
或是数据库本身不支持, 如MySQL的Myisam.
不防看看是不是由多数据源引起的:
多数据源我们一般这样配置:
@Configuration
@EnableDataSource("A")
@EnableJpaRepositories(
repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class,
entityManagerFactoryRef = "defaultEntityManagerFactory",
basePackages = {"com.A.dao"},
transactionManagerRef = "defaultTransactionManager"
)
@ConditionalOnProperty(
prefix = "XXX.datasource.default",
name = {"driverClassName", "url", "username", "password"}
)
protected static class DataSourceDefaultConfiguration {
private static final Logger log = LoggerFactory.getLogger(DataSourceConfiguration.DataSourceDefaultConfiguration.class);
protected DataSourceDefaultConfiguration() {
}
}
@Configuration
@EnableDataSource("B")
@EnableJpaRepositories(
repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class,
entityManagerFactoryRef = "myGblEntityManagerFactory",
basePackages = {"com.B.dao"},
transactionManagerRef = "myGblTransactionManager"
)
@ConditionalOnProperty(
prefix = "XXX.datasource.myGbl",
name = {"driverClassName", "url", "username", "password"}
)
protected static class DataSourceMyGblConfiguration {
private static final Logger log = LoggerFactory.getLogger(DataSourceConfiguration.DataSourceMyGblConfiguration.class);
protected DataSourceMyGblConfiguration() {
}
}
这样, 其中一个数据源就是默认的数据源, 如A. 如果使用@Transaction注解开启事务, 默认使用默认数据源A来进行开启, 提交, 回滚 事务, 这个时候如果@Transaction方法里面是对com.B.dao进行的操作, 那么其实事务也是作用在A数据源上, 就会产生事务不生效的假象.
这时需指定事务对象来声明事务: @Transactional(value = "myGblTransactionManager"), 这时事务会作用在com.B.dao的操作上.
关于事务是否生效, 可以从这里入手检查:
org.springframework.orm.jpa.JpaTransactionManager
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
JpaTransactionObject txObject = (JpaTransactionObject) transaction;
if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
throw new IllegalTransactionStateException(
"Pre-bound JDBC Connection found! JpaTransactionManager does not support " +
"running within DataSourceTransactionManager if told to manage the DataSource itself. " +
"It is recommended to use a single JpaTransactionManager for all transactions " +
"on a single DataSource, no matter whether JPA or JDBC access.");
}
try {
if (!txObject.hasEntityManagerHolder() ||
txObject.getEntityManagerHolder().isSynchronizedWithTransaction()) {
如果是非JpaTransactionManager, 可以从其接口入手PlatformTransactionManager
分享到:
相关推荐
Spring在Transaction事务传播行为种类,希望对大家有所帮助
spring事务管理注解jar,spring-tx-3.2.4.RELEASE.jar,导入项目即可
spring-transaction.jar.zip
spring 事务管理工程,导入到MyEclipse 中既可以使用
spring-tx spring-transaction spring事务管理实例
Transaction Coordinator(TC) :事务协调器,维护全局事务的运行状态,负责协调并驱动全局事务的提交或回滚; Transaction Manager(TM) :控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚...
Spring中的事务管理问题:org.springframework.transaction-3.1.0.M1.jar包。org.springframework.transaction-3.1.0.M1.jar包。
org.springframework.transaction-3.2.2.RELEASE最新版本
测试spring事务管理 搭建了ssh框架的web工程 本工程用到的数据库表很简单 user(id, name) 可自行创建 本例所有的事务放在service层进行管理,方法中间抛出运行时异常以测试是否回滚 Spring配置文件中关于事务...
org.springframework.transaction-3.0.0.M4.jar
里面为一个演示spring事务传播机制的小demo。简单实现转账功能,通过添加注解调整传播级别,同时通过日志打印查看sql是否执行,在mysql中的数据是否发生了变化(操作提交还是回滚了)。
NULL 博文链接:https://wsjjasper.iteye.com/blog/1570448
Spring交易演示spring-transaction-demo spring事务传播机制的7种使用方式
org.springframework.transaction-3.1.0.M2
分布式事务框架LCN支持springcloud 2.0.5 ,transaction-springcloud-4.1.2.jar
org.springframework.transaction-3.2.4.RELEASE.jar,最新版的org.springframework.transaction,Wed Aug 07 16:44:37 GMT+01:00 2013
org.springframework.transaction-3.1.0.M1.jar
spring 集成框架基础问题springboot + shiro @Transaction不生效SpringTransactionAnnotationPars
Spring中的@Transactional事物回滚实例源码
Spring 针对 Java Transaction API (JTA)、JDBC、Hibernate 和 Java Persistence API (JPA) 等事务 API,实现了一致的编程模型,而 Spring 的声明式事务功能更是提供了极其方便的事务配置方式,配合 Spring Boot 的...