Spring事务:在Exception或Throwable上回滚 [英] Spring transaction: rollback on Exception or Throwable

查看:1493
本文介绍了Spring事务:在Exception或Throwable上回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知是否有意义,而不是使用

I wonder whether it makes sense to use instead of

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)

使用 Throwable

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)

据我所知,捕捉错误将帮助我们正确行事,即使发生了非常糟糕的事情。或者它可能没有帮助?

As I understand catching Error will help us behave correctly even when something really bad happen. Or maybe it wouldn't help?

推荐答案


据我所知捕捉错误将帮助我们正确行事即使发生了非常糟糕的事情。或者它可能没有帮助?

As I understand catching Error will help us behave correctly even when something really bad happen. Or maybe it wouldn't help?

您不需要显式指定 rollbackFor = Throwable.class ,因为如果发生错误,spring将默认回滚事务。

You don't need to explicitly specify rollbackFor = Throwable.class, because spring will by default rollback the transaction if an Error occurs.

参见 12.5.3回滚声明性事务


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

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.

或者查看 DefaultTransactionAttribute

public boolean rollbackOn(Throwable ex) {
    return (ex instanceof RuntimeException || ex instanceof Error);
}

这篇关于Spring事务:在Exception或Throwable上回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆