Spring 春季需要的传播与嵌套传播?
实际查找嵌套的传播(如果当前事务存在,则在嵌套事务中执行)和必需的传播(支持当前事务)之间的差异 下面是简单的用例 假设在main类中,我们调用method1并使用jdbc[Transaction1]创建客户。尚未提交。现在我们在main类中调用method2,并为刚刚创建的客户[Transaction2]创建帐户。现在付诸行动。我们可以将事务2称为嵌套事务吗 根据我现在的理解,如果我们将事务定义定义为Spring 春季需要的传播与嵌套传播?,spring,transactions,Spring,Transactions,实际查找嵌套的传播(如果当前事务存在,则在嵌套事务中执行)和必需的传播(支持当前事务)之间的差异 下面是简单的用例 假设在main类中,我们调用method1并使用jdbc[Transaction1]创建客户。尚未提交。现在我们在main类中调用method2,并为刚刚创建的客户[Transaction2]创建帐户。现在付诸行动。我们可以将事务2称为嵌套事务吗 根据我现在的理解,如果我们将事务定义定义为 事务2将被视为嵌套的,但如果我们将其定义为所需的,它将支持当前事务。那么嵌套的和必需的有什么
事务2将被视为嵌套的,但如果我们将其定义为所需的,它将支持当前事务。那么嵌套的和必需的有什么区别呢?传播\u嵌套只能与
数据源TransactionManager
和JDBC3驱动程序一起使用。它使用保存点,以便能够回滚事务的某些部分(即Spring术语中嵌套事务的组成部分)。请参见以了解保存点的工作方式
要求完全不同。它只是意味着:如果已经存在一个事务,则在该事务中执行工作;否则,启动一个新事务,完成工作,并提交事务。因为您使用的是Spring,具有必需的/嵌套的传播,所以问题中提到的[Transaction1]和[Transaction2]是“相同的事务” 作为您的用例,如果您在method2()上使用“required”
如果使用嵌套在method2()上
嵌套事务的用例 (当一个客户需要一百万个帐户,并且需要几个小时才能完成所有任务时) 收益=>
以这个例子为例,我们在main类中调用method1,并使用jdbc[Transaction1]创建客户。尚未提交。现在我们在main类中调用method2,并为刚刚创建的客户[Transaction2]创建帐户。现在付诸行动。根据您的解释,这两个事务都将被视为一个事务的一部分(因为一个连接最多只能有一个事务),但如果我们使用保存点部分回滚或提交,我们将事务2称为嵌套事务。这是否正确?
@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
}
@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
}