Skip to content

Commit a06aec1

Browse files
authored
Merge pull request #2606 from JoeyChanMiao/main
[doc fix]修正事务传播行为中TransactionDefinition.PROPAGATION_NESTED案例描述错误
2 parents b93f138 + 3104392 commit a06aec1

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

docs/system-design/framework/spring/spring-transaction.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -424,28 +424,56 @@ Class B {
424424
- 在外部方法开启事务的情况下,在内部开启一个新的事务,作为嵌套事务存在。
425425
- 如果外部方法无事务,则单独开启一个事务,与 `PROPAGATION_REQUIRED` 类似。
426426

427-
这里还是简单举个例子:如果 `bMethod()` 回滚的话,`aMethod()`不会回滚。如果 `aMethod()` 回滚的话,`bMethod()`会回滚。
428-
429-
```java
430-
@Service
431-
Class A {
432-
@Autowired
433-
B b;
434-
@Transactional(propagation = Propagation.REQUIRED)
435-
public void aMethod {
436-
//do something
437-
b.bMethod();
427+
举个例子:
428+
- 如果 `aMethod()` 回滚的话,作为嵌套事务的`bMethod()`会回滚。
429+
- 如果 `bMethod()` 回滚的话,`aMethod()`是否回滚,要看`bMethod()`的异常是否被处理:
430+
- `bMethod()`的异常没有被处理,即`bMethod()`内部没有处理异常,且`aMethod()`也没有处理异常,那么`aMethod()`将感知异常致使整体回滚。
431+
```java
432+
@Service
433+
Class A {
434+
@Autowired
435+
B b;
436+
@Transactional(propagation = Propagation.REQUIRED)
437+
public void aMethod (){
438+
//do something
439+
b.bMethod();
440+
}
438441
}
439-
}
440-
441-
@Service
442-
Class B {
443-
@Transactional(propagation = Propagation.NESTED)
444-
public void bMethod {
445-
//do something
442+
443+
@Service
444+
Class B {
445+
@Transactional(propagation = Propagation.NESTED)
446+
public void bMethod (){
447+
//do something and throw an exception
448+
}
446449
}
447-
}
448-
```
450+
```
451+
- `bMethod()`处理异常或`aMethod()`处理异常,`aMethod()`不会回滚。
452+
453+
```java
454+
@Service
455+
Class A {
456+
@Autowired
457+
B b;
458+
@Transactional(propagation = Propagation.REQUIRED)
459+
public void aMethod (){
460+
//do something
461+
try {
462+
b.bMethod();
463+
} catch (Exception e) {
464+
System.out.println("方法回滚");
465+
}
466+
}
467+
}
468+
469+
@Service
470+
Class B {
471+
@Transactional(propagation = Propagation.NESTED)
472+
public void bMethod {
473+
//do something and throw an exception
474+
}
475+
}
476+
```
449477

450478
**4.`TransactionDefinition.PROPAGATION_MANDATORY`**
451479

0 commit comments

Comments
 (0)