mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: 嵌套事务下,且传播方式为 REQUIRED 时,当子事务抛出异常时出错的问题;close #I7N8A1
This commit is contained in:
parent
0203cb76bd
commit
34ff28dfcc
@ -25,6 +25,7 @@ import org.springframework.transaction.support.DefaultTransactionStatus;
|
||||
|
||||
/**
|
||||
* MyBatis-Flex 事务支持。
|
||||
* @author michael
|
||||
*/
|
||||
public class FlexTransactionManager extends AbstractPlatformTransactionManager {
|
||||
|
||||
@ -70,6 +71,13 @@ public class FlexTransactionManager extends AbstractPlatformTransactionManager {
|
||||
TransactionalManager.rollback(transactionObject.currentXid);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException {
|
||||
// 在多个事务嵌套时,子事务的传递方式为 REQUIRED(加入当前事务)
|
||||
// 那么,当子事务抛出异常时,会调当前方法,而不是直接调用 doRollback
|
||||
// 此时,不需要做任何处理即可,默认上父级事务进行正常 doRollback 即可。
|
||||
}
|
||||
|
||||
|
||||
static class TransactionObject {
|
||||
|
||||
|
||||
@ -82,17 +82,13 @@ public class AccountController {
|
||||
Row row2 = Db.selectOneById(null, "tb_account", "id", 2);
|
||||
System.out.println(">>>>>>> row2: " + row2);
|
||||
|
||||
// Account account1 = accountMapper.selectOneById(1L);
|
||||
// Account account2 = accountMapper.selectOneById(2L);
|
||||
//
|
||||
// accountService.update2();
|
||||
//
|
||||
// if (true) {
|
||||
// throw new IllegalStateException("aaa");
|
||||
// }
|
||||
//
|
||||
// System.out.println("selectOne >>>> " + account1);
|
||||
// System.out.println("selectOne >>>> " + account2);
|
||||
Account account = new Account();
|
||||
account.setId(2L);
|
||||
account.setUserName("haha1111");
|
||||
accountMapper.update(account);
|
||||
|
||||
//嵌套事务
|
||||
accountService.update2();
|
||||
|
||||
return accountMapper.selectOneById(id);
|
||||
}
|
||||
|
||||
@ -19,7 +19,6 @@ package com.mybatisflex.test.service;
|
||||
import com.mybatisflex.test.mapper.AccountMapper;
|
||||
import com.mybatisflex.test.model.Account;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@ -32,8 +31,9 @@ public class AccountService {
|
||||
AccountMapper accountMapper;
|
||||
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Transactional
|
||||
public void update2() {
|
||||
int x = 1/0;
|
||||
Account account = new Account();
|
||||
account.setId(2L);
|
||||
account.setUserName("haha");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user