我有一个场景,我想在只读事务中执行儿童1,在读写事务中执行儿童2,两者都在一个过拱形事务中。如果儿童1或儿童2出现问题,我只想回滚过拱形事务。
@Transactional
parentMethod(){
TransactionSynchronizationManager
.setCurrentTransactionName("TestTransaction");
log.info("Current Transaction Name - parentMethod ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - parentMethod ::: " +
TransactionSynchronizationManager.isCurrentTransactionReadOnly());
child1();
child2();
}
@Transactional(readOnly=true)
child1(){
log.info("Current Transaction Name - child1 ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - child1 ::: " +
TransactionSynchronizationManager
.isCurrentTransactionReadOnly());
}
@Transactional
child2(){
log.info("Current Transaction Name - child2 ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - child2 ::: " +
TransactionSynchronizationManager
.isCurrentTransactionReadOnly());
}
输出日志显示:
Current Transaction Name - parentMethod ::: TestTransaction
Is the transaction readonly - parentMethod ::: false
Current Transaction Name - child1 ::: null
Is the transaction readonly - child1 ::: false
Current Transaction Name - child2 ::: null
Is the transaction readonly - child2 ::: false
然而,如果我在child 1方法中故意设置TransactionSyn行吗izationManager. setCurrentTransactionReadOnly(true),则输出会像我预期的那样略有变化。
@Transactional(readOnly=true)
child1(){
TransactionSynchronizationManager
.setCurrentTransactionReadOnly(true);
log.info("Current Transaction Name - child1 ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - child1 ::: " +
TransactionSynchronizationManager
.isCurrentTransactionReadOnly());
}
输出日志显示:
Current Transaction Name - parentMethod ::: TestTransaction
Is the transaction readonly - parentMethod ::: false
Current Transaction Name - child1 ::: null
Is the transaction readonly - child1 ::: true
Current Transaction Name - child2 ::: null
Is the transaction readonly - child2 ::: false
我只是想知道我正在做的事情是否有问题。或者有没有更好的方法来做到这一点。但是,目前的事务名称在儿童1和儿童2中仍然为空,我希望它是“TestTransaction”,这是在父母方法中设置的。
此外,只是想知道哪一个优先,只读还是传播级别,当有嵌套事务时,这些值设置不同。
@Transactional
parentMethod(){
TransactionSynchronizationManager
.setCurrentTransactionName("TestTransaction");
log.info("Current Transaction Name - parentMethod ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - parentMethod ::: " +
TransactionSynchronizationManager.isCurrentTransactionReadOnly());
child1();
child2();
}
child1(){
TransactionSynchronizationManager
.setCurrentTransactionReadOnly(true);
log.info("Current Transaction Name - child1 ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - child1 ::: " +
TransactionSynchronizationManager
.isCurrentTransactionReadOnly());
TransactionSynchronizationManager
.setCurrentTransactionReadOnly(false);
}
child2(){
log.info("Current Transaction Name - child2 ::: " +
TransactionSynchronizationManager
.getCurrentTransactionName());
log.info("Is the transaction readonly - child2 ::: " +
TransactionSynchronizationManager
.isCurrentTransactionReadOnly());
}
这似乎对我有用。我在这里混合了程序化和声明式的处理事务的方式。我只有一个在父母方法
上的拱@Transactional
注释,并在需要时显式设置和取消设置readonly
。有人能对此发表评论或意见吗?