Spring:对检查到的异常进行自动回滚 [英] Spring: automatic rollback on checked exceptions

查看:76
本文介绍了Spring:对检查到的异常进行自动回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Spring配置为在非 RuntimeExceptions 上回滚的一种方法是在服务类上使用 @Transactional(rollbackFor = ...)批注.这种方法的问题在于,我们需要为几乎所有的服务类定义(rollbackFor = ...),这似乎确实很多余.

One way to configure Spring to rollback on a non RuntimeExceptions is using @Transactional(rollbackFor=...) annotation on the service classes. The problem with this approach is that we need to define (rollbackFor=...) for almost all the service classes which seems really redundant.

我的问题:有什么方法可以配置Spring事务管理器的默认行为,使其在非 RuntimeException 发生时回滚,而无需在每个 @Transactional 批注上声明它.类似于在EJB中的异常类上使用 @ApplicationException(rollback = true)注释.

My question: Is there any way to configure a default behaviour for Spring transaction manager to rollback on a non RuntimeException whenever it happens without declaring it on every @Transactional annotation. Something like using @ApplicationException(rollback=true) annotation on an exception class in EJB.

推荐答案

您无法使用@Transactional在应用程序级别执行此操作,但是您可以:

You can't do it for application level with @Transactional , but you can :

变体1:扩展@Transactional批注并将其作为rollbackfor的默认值.但是只设置您需要的rollbackFor未检查的异常即可.只有这样,您才能控制回滚,并避免复制@Transactional(rollbackFor = MyCheckedException.class)

variant 1 : extend @Transactional annotation and put it as default value for rollbackfor. But set rollbackFor unchecked exceptions only that you need .With this you can control rollbacks only for case that you sure , and avoid copy past of @Transactional(rollbackFor =MyCheckedException.class)

赞:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(rollbackFor=MyCheckedException.class)
public @interface TransactionalWithRollback {
}

并使用此注释代替标准的@Transactional.

And use this annotation instead of standard @Transactional.

变体2:,您可以从

variant 2 : you can create extension from AnnotationTransactionAttributeSource and override method determineTransactionAttribute:

protected TransactionAttribute  determineTransactionAttribute(AnnotatedElement ae)
//Determine the transaction attribute for the given method or class.

TransactionAttribute参见 TransactionAttribute api,有一种方法

TransactionAttribute see TransactionAttribute api , there is a method

boolean rollbackOn(Throwable ex)我们应该回退给定的异常吗?

boolean rollbackOn(Throwable ex) Should we roll back on the given exception?

protected TransactionAttribute determineTransactionAttribute(
    AnnotatedElement ae) {
    return new DelegatingTransactionAttribute(target) {
        @Override
        public boolean rollbackOn(Throwable ex) {
           return (check is exception type as you need for rollback );
       }
};

}

第二种方法不如您首先使用,因为它对于事务管理器来说确实是全球性的.更好地使用自定义注释,因为您可以对其进行控制,仅适用于真正需要它的方法/类.但是,如果您在任何情况下都需要使用第二种变体,它将是您的默认跨国行为.

Second approach is not so good as first as you do it really global for transaction manager. Better use custom annotation as you can control it any apply only for methods/classes where you really need it. But if you need it in any case use second variant , it will be your default transnational behavior.

这篇关于Spring:对检查到的异常进行自动回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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