gpt4 book ai didi

spring - 当受检异常在事务中传播时会发生什么?

转载 作者:行者123 更新时间:2023-12-01 02:31:50 33 4
gpt4 key购买 nike

我知道事务管理器中的默认回滚策略是在发生 RuntimeExceptions 时自动回滚。但是,如果我有一个允许传播的已检查异常会发生什么?

最佳答案

However, please note that the Spring Framework's transaction infrastructure code will, by default, only mark 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 will not result in the transaction being rolled back.



Source

即使抛出受检异常,Spring 也会提交事务,就好像一切正​​常一样。
如果需要,您可以配置对已检查异常的回滚。
喜欢
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="false" rollback-for="AnyCheckedException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

将在 AnyCheckedException 时执行回滚被抛出。

这可能会有所帮助: Spring transaction management with checked and unchecked exception

关于spring - 当受检异常在事务中传播时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12699189/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com