到底什么场景会使用到Transactional(rollbackFor = Exception.class)?
1
Paosin Apr 17, 2021
学到了😂
|
2
siweipancc Apr 17, 2021 via iPhone
……不明所以
|
3
JasonLaw OP @siweipancc #2 ?
|
4
xuanbg Apr 17, 2021
这篇文章里面的示例,调用同一个类的方法,事务不起作用的吧。
|
5
siweipancc Apr 17, 2021 via iPhone
@JasonLaw :D 又重看了一次,我还是看不懂
|
6
crclz Apr 17, 2021
根据 https://www.cnblogs.com/clwydjgs/p/9317849.html:
在 @Transactional 注解中如果不配置 rollbackFor 属性,那么事物只会在遇到 RuntimeException 的时候才会回滚,加上 rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚。 可以理解是,假设你始终使用 RuntimeException (遵循 kotlin 和 C#的风格),那么就只需要用 @Transactional 。我的建议是:Exception 有害,请遵循 kotlin 和 C#的风格。 |
7
JasonLaw OP @xuanbg #4 虽然 transferMoney 调用了同一个 class 中的方法,但是是外部调用 transferMoney 的,所以`@Transactional`是起作用的。
|
8
JasonLaw OP @siweipancc #5 你说的看是看什么😅?
|
10
securityCoding Apr 17, 2021
推荐看一下 @Transactional 的原理.
本质上 jdbc 是通过捕捉异常来判断是否需要回滚,所以才有指定异常回滚这种用法 |
12
jin7 Apr 17, 2021
@transactional(rollbackfor = throwable.class) 有没有必要?
|
13
taogen Apr 17, 2021 @jin7 #12
"In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration." - https://docs.spring.io/spring-framework/docs/3.2.6.RELEASE/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back 1. default rollback for unchecked exceptions 2. Errors will also - by default - result in a rollback So recommend specifying rollback for Excpetion class. |
14
JasonLaw OP @taogen #13 "So recommend specifying rollback for Excpetion class."是在哪里出现的?没有找到
|
15
bxb100 Apr 17, 2021 via Android
一般都是全捕捉然后回滚
|
17
ychost Apr 19, 2021
我一直觉得 Java 的 checked Exception 有点鸡肋,看上去很能帮助用户意识到方法可能会抛什么 exception,但是真实的工程项目里面用着贼操蛋
|