实际查找PROPAGATION_NESTED(如果当前事务存在,则在嵌套事务中执行)和PROPAGATION_Required(支持当前事务)之间的差异。
下面是简单的用例
假设在主类中,我们调用method1并使用jdbcTransaction1创建客户。没有提交yet.now,我们调用主类中的method2并为刚刚创建的customerTransaction2创建帐户。现在提交它。我们可以将事务2称为嵌套事务吗?
根据我现在的理解,如果我们将事务定义为PROPAGATION_NESTED
事务2将被视为嵌套的,但如果我们将其定义为PROPAGATION_Required,它将支持当前事务。那么嵌套和required有什么区别呢?
发布于 2011-07-14 03:04:58
PROPAGATION_NESTED只能与DataSourceTransactionManager和JDBC3驱动程序一起使用。它使用保存点,以便能够回滚事务的某一部分(即,在Spring术语中构成嵌套事务的部分)。请参阅javadoc of Connection了解存储点是如何工作的。
REQUIRED是完全不同的。它只意味着:如果已经存在事务,则在此事务中执行工作;否则,启动一个新事务,完成工作,然后提交事务。
发布于 2020-04-15 14:54:25
因为您使用的是带有必需/嵌套传播的Spring,所以问题中提到的Transaction1和Transaction2是“相同的事务”。
作为您的用例,如果您在method2()上使用"required“
@Transaction(Require)
main() {
// throw new Exception(); => rollback all
method1();
method2();
// throw new Exception(); => rollback all
}
@Transaction(Require)
method1() {
// throw new Exception(); => rollback all
}
@Transaction(Require)
method2() {
// throw new Exception(); => rollback all
}如果使用嵌套的method2()
@Transaction(Require)
main() {
// throw new Exception(); => rollback all
method1();
// Create Save Point A
method2();
// Release Save Point A
// throw new Exception(); => rollback all
}
@Transaction(Require)
method1() {
// throw new Exception(); => rollback all
}
@Transaction(Nested) // is the same transaction as main
method2() {
// throw new Exception(); => will only rollback to Save Point A
}嵌套事务的用例
(当一个客户需要一百万个账号,需要几个小时才能完成所有任务)
收益=>
相比
例如:
@Transaction(Require)
main() {
// throw new Exception(); => rollback all
method1();
for(many time) {
// Create Save Point
method2();
// Release Save Point
}
// throw new Exception(); => rollback all (Be careful, it will rollback all!!!)
}
@Transaction(Require)
method1() {
// throw new Exception(); => rollback all
}
@Transaction(Nested) // is the same transaction as main
method2() {
// throw new Exception(); => will only rollback to Save Point
}https://stackoverflow.com/questions/6683929
复制相似问题