spring 声明式 rollback-for 的示例?

发布于 2024-09-26 23:16:38 字数 530 浏览 1 评论 0原文

想要 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="" ,有什么方法或其他方法吗? 如果方法那么该方法将放在哪里?

want declarative transactional management example in spring aop........

Actually Here

<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>

So here what i want to write actually rollback-for="" , is there any method or any else?
and if method then where will it that method put?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-10-03 23:16:38

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

rollback-for="your.pkg.NoProductInStockException"

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

交易文档解释:

向 Spring 框架的事务基础结构指示要回滚事务工作的推荐方法是从当前在事务上下文中执行的代码中抛出异常。 Spring 框架的事务基础结构代码将在调用堆栈中冒泡时捕获任何未处理的异常,并确定是否将事务标记为回滚。

在其默认配置中,Spring 框架的事务基础结构代码仅在运行时、未检查异常的情况下标记要回滚的事务;也就是说,当抛出的异常是 RuntimeException 的实例或子类时。 (默认情况下,错误也会导致回滚)。从事务方法引发的已检查异常不会导致默认配置中的回滚。

您可以准确配置哪些异常类型将事务标记为回滚,包括已检查的异常。

in rollback-for you specify the name of the exception. For example if you you want to rollback for your.pkg.NoProductInStockException, you write

rollback-for="your.pkg.NoProductInStockException"

This will make the transaction be rolled back if it encounters an exception that matches the specified. If an exception is thrown that does not match, it is propagated to the caller of the service or wrapped into a TransactionRolledBackException

The transaction documentation explains:

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.

很糊涂小朋友 2024-10-03 23:16:38

默认情况下,对于未经检查的异常不需要执行此操作。如果在方法内抛出未经检查的异常,则必须使用 rollback-for 属性。您可以使用正则表达式样式,例如:*InStockException

By default, there should be no need to do this for unchecked exceptions. If you throw an unchecked exception inside the method then you have to use the rollback-for attribute. You can use regular expression style, e.g.: *InStockException

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文