From 3104392b9a53167660d2c4e0132964d7a014d95d Mon Sep 17 00:00:00 2001 From: JoeyChan Date: Fri, 14 Feb 2025 22:46:50 +0800 Subject: [PATCH] =?UTF-8?q?[doc=20fix]=E4=BF=AE=E6=AD=A3=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BC=A0=E6=92=AD=E8=A1=8C=E4=B8=BA=E4=B8=ADTransactionDefinit?= =?UTF-8?q?ion.PROPAGATION=5FNESTED=E6=A1=88=E4=BE=8B=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/spring/spring-transaction.md | 68 +++++++++++++------ 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/docs/system-design/framework/spring/spring-transaction.md b/docs/system-design/framework/spring/spring-transaction.md index 47eac108..b294d6f6 100644 --- a/docs/system-design/framework/spring/spring-transaction.md +++ b/docs/system-design/framework/spring/spring-transaction.md @@ -424,28 +424,56 @@ Class B { - 在外部方法开启事务的情况下,在内部开启一个新的事务,作为嵌套事务存在。 - 如果外部方法无事务,则单独开启一个事务,与 `PROPAGATION_REQUIRED` 类似。 -这里还是简单举个例子:如果 `bMethod()` 回滚的话,`aMethod()`不会回滚。如果 `aMethod()` 回滚的话,`bMethod()`会回滚。 - -```java -@Service -Class A { - @Autowired - B b; - @Transactional(propagation = Propagation.REQUIRED) - public void aMethod { - //do something - b.bMethod(); +举个例子: +- 如果 `aMethod()` 回滚的话,作为嵌套事务的`bMethod()`会回滚。 +- 如果 `bMethod()` 回滚的话,`aMethod()`是否回滚,要看`bMethod()`的异常是否被处理: + - `bMethod()`的异常没有被处理,即`bMethod()`内部没有处理异常,且`aMethod()`也没有处理异常,那么`aMethod()`将感知异常致使整体回滚。 + ```java + @Service + Class A { + @Autowired + B b; + @Transactional(propagation = Propagation.REQUIRED) + public void aMethod (){ + //do something + b.bMethod(); + } } -} - -@Service -Class B { - @Transactional(propagation = Propagation.NESTED) - public void bMethod { - //do something + + @Service + Class B { + @Transactional(propagation = Propagation.NESTED) + public void bMethod (){ + //do something and throw an exception + } } -} -``` + ``` + - `bMethod()`处理异常或`aMethod()`处理异常,`aMethod()`不会回滚。 + + ```java + @Service + Class A { + @Autowired + B b; + @Transactional(propagation = Propagation.REQUIRED) + public void aMethod (){ + //do something + try { + b.bMethod(); + } catch (Exception e) { + System.out.println("方法回滚"); + } + } + } + + @Service + Class B { + @Transactional(propagation = Propagation.NESTED) + public void bMethod { + //do something and throw an exception + } + } + ``` **4.`TransactionDefinition.PROPAGATION_MANDATORY`**