@Transactional 在不同的事务管理器上是如何工作的?

huangapple go评论57阅读模式
英文:

How @Transactional working on different transaction manager?

问题

我有多个数据库,所以我创建了多个事务管理器来管理它们的事务。

我听说 @Transactional 的默认传播级别是 Propagation.REQUIRED

如果传播级别是 REQUIRED,子事务将加入到父事务中。

但如果是这样,子事务的事务管理器将变成父事务管理器。

这正确吗?还是不同的事务管理器会创建自己的事务?

@RequiredArgsConstructor
@Transactional(transactionManager = "mainTransactionManager")
public class MainService {

  private final MainDao mainDao;

  private final SubService subService;

  public void doMain() {
    String result = "Fail";
    try {
      mainDao.update(); // 这里出现异常,mainTransactionManager 将回滚
      result = "Success";
    } finally {
      subService.doSub(result); // 这里出现异常,mainTransactionManager 和 subTransactionManager 都会回滚
    }
  }

}

@RequiredArgsConstructor
@Transactional(transactionManager = "subTransactionManager")
public class SubService {

  private final SubDao subDao;

  public void doSub(String result) {
    subDao.update(result);
  }

}
英文:

I have multiple databases, so I created multiple transaction manager to manage their transaction.

I've heared that @Transactional's default propagation level is Propagation.REQUIRED.

If propagation level is REQUIRED, the child transaction will join to parent transaction.

But if so, the child transaction's transaction manager will became parent transaction manager.

Does it correct? Or does different transaction manager creates their own transaction?

@RequiredArgsConstructor
@Transactional( transactionManager = "mainTransactionManager" )
public class MainService {
  
  private final MainDao mainDao;

  private final SubService subService;

  public void doMain() {
     String result = "Fail";
     try {
       mainDao.update(); // Exception on here, mainTransactionManager will rollback
       result = "Success"
     } finally {
       subService.doSub( result ); // Exception on here, mainTransactionManager & subTransactionManager will rollback
     }
  }

}

@RequiredArgsConstructor
@Transactional( transactionManager = "subTransactionManager" )
public class SubService {
  
  private final SubDao subDao;

  public void doSub( String result ) {
     subDao.update( result );
  }

}

答案1

得分: 1

你可以尝试使用 ChainedTransactionManager:https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/transaction/ChainedTransactionManager.html

@Bean 
PlatformTransactionManager chainedTransactionManager() {
  return new ChainedTransactionManager(mainTransactionManager, subTransactionManager);
}

然后在两个方法中使用它。

英文:

Not sure but you can try ChainedTransactionManager: https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/transaction/ChainedTransactionManager.html

@Bean 
PlatformTransactionManager chainedTransactionManager() {
  return new ChainedTransactionManager(mainTransactionManager, subTransactionManager);
}

And use it over both methods

答案2

得分: 0

这取决于您的需求背景。如果您希望保持数据库中的事务独立,那么您将需要使用 REQUIRES_NEW。否则,如果您希望它们在同一事务块下管理,那么REQUIRED就足够了。

英文:

It depends on the context of your requirements. If you want to keep the transactions in your DBs independent, then you will have to use REQUIRES_NEW. Otherwise, REQUIRED would suffice if you want them to be managed under the same transactional block.

huangapple
  • 本文由 发表于 2023年5月25日 12:26:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定