本文介绍了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?
推荐答案
您不需要显式指定 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.
参见
或者查看 DefaultTransactionAttribute
public boolean rollbackOn(Throwable ex) {
return (ex instanceof RuntimeException || ex instanceof Error);
}
这篇关于Spring事务:在Exception或Throwable上回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!