gpt4 book ai didi

java - Spring 声明回滚的例子?

转载 作者:行者123 更新时间:2023-11-29 08:17:02 25 4
gpt4 key购买 nike

想在spring aop中声明式事务管理的例子.......

真的在这里

<aop:config>
<aop:advisor advice-ref="addAdvice" pointcut="execution(* com.DAO.*.*(..))"/>
</aop:config>
<tx:advice id="addAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="" />
</tx:attributes>
</tx:advice>

所以这里我想写的实际上是 rollback-for="",有什么方法吗?如果有方法,那么该方法会放在哪里?

最佳答案

rollback-for 中指定异常的名称。例如,如果您想回滚 your.pkg.NoProductInStockException,您可以编写

rollback-for="your.pkg.NoProductInStockException"

这将使事务在遇到与指定匹配的异常时回滚。如果抛出不匹配的异常,它会传播到服务的调用者或包装到 TransactionRolledBackException

The transaction documentation解释:

The recommended way to indicate to the Spring Framework's transaction infrastructure that a transaction's work is to be rolled back is to throw an Exception from code that is currently executing in the context of a transaction. The Spring Framework's transaction infrastructure code will catch any unhandled Exception as it bubbles up the call stack, and make a determination whether to mark the transaction for rollback.

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.

You can configure exactly which Exception types mark a transaction for rollback, including checked exceptions.

关于java - Spring 声明回滚的例子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3881378/

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