spring事务-Exception不会回滚

在Spring中,事务管理默认情况下只会在遇到运行时异常(RuntimeException)和错误(Error)时进行回滚。对于检查型异常(Checked Exception),Spring事务默认不会进行回滚。

如果你想让Spring事务在遇到检查型异常时也进行回滚,你可以采取以下几种方法:

方法一:使用@Transactional注解的rollbackFor属性

你可以在@Transactional注解中指定哪些异常发生时应该触发事务回滚,包括检查型异常。

java 复制代码
import org.springframework.transaction.annotation.Transactional;


@Service
public class MyService {


    @Transactional(rollbackFor = Exception.class)
    public void myMethod() throws Exception {
        // ... 业务逻辑 ...
        throw new Exception("发生了一个检查型异常");
    }
}

在上面的例子中,rollbackFor = Exception.class表示任何类型的异常(包括运行时异常和检查型异常)都会触发事务回滚。

方法二:全局配置

如果你想对整个应用进行全局配置,让所有的检查型异常都触发事务回滚,你可以在Spring配置文件中进行如下设置:

xml 复制代码
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" rollback-for="java.lang.Exception"/>
    </tx:attributes>
</tx:advice>


<aop:config>
    <aop:pointcut id="serviceOperation" expression="execution(* com.yourpackage..*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>

或者在Java配置类中使用@EnableTransactionManagementTransactionInterceptor

java 复制代码
@Configuration
@EnableTransactionManagement
public class TransactionConfig implements TransactionManagementConfigurer {


    @Autowired
    private PlatformTransactionManager transactionManager;


    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return transactionManager;
    }


    @Bean
    public TransactionInterceptor txInterceptor() {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute();
        transactionAttribute.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        source.addTransactionalMethod("*", transactionAttribute);
        return new TransactionInterceptor(transactionManager, source);
    }
}

Java

方法三:编程式事务管理

如果你使用编程式事务管理,可以在调用TransactionStatussetRollbackOnly方法来标记事务需要回滚。

java 复制代码
@Service
public class MyService {


    @Autowired
    private PlatformTransactionManager transactionManager;


    public void myMethod() {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            // ... 业务逻辑 ...
            throw new Exception("发生了一个检查型异常");
        } catch (Exception ex) {
            status.setRollbackOnly();
        } finally {
            transactionManager.commit(status);
        }
    }
}

注意事项

  • 使用rollbackFor属性时要小心,确保不会因为过于宽泛的异常捕获导致意外的数据不一致。
  • 在设计服务层时,应该尽量避免抛出检查型异常,而是使用运行时异常来表示错误情况,这样可以更好地利用Spring的事务管理机制。

通过上述方法,你可以控制Spring事务在不同异常情况下的行为,确保数据的完整性和一致性。

相关推荐
m0_5873830011 小时前
全民健身解决方案软件开发实战:从架构设计到落地指南
java·spring boot·spring·架构·需求分析
wno70412 小时前
Spring Cloud Hystrix服务容错
spring·spring cloud·hystrix
用户274621291728014 小时前
深入 Spring AI ChatClient:从一行代码到 LLM 调用的完整旅程
spring
麻瓜code19 小时前
【Agent】Spring AI RAG 实战:本地向量库 + 云端知识库
java·人工智能·spring
水巷石子20 小时前
学习langChain4j的第二天,体验springBoot中的starter
java·spring boot·学习·spring·langchain4j
ly768920 小时前
Spring Bean生命周期全流程:从BeanDefinition到销毁
java·后端·spring·bean生命周期·beandefinition·初始化回调
计算机学姐21 小时前
基于SpringBoot的旅游系统的设计与实现
java·vue.js·spring boot·后端·spring·java-ee·旅游
大模型码小白1 天前
【AI大模型】DeepSeek Harness 深度解析:大模型评测框架的架构与实践
java·运维·人工智能·spring·架构·自动化
苏渡苇1 天前
Spring Insight 里上报 Span 为什么用 JDK HttpClient 而不是 RestTemplate
java·后端·spring·spring cloud·springboot·性能监控
卓怡学长1 天前
w157基于springboot北部湾地区助农平台
java·spring boot·spring·maven·intellij-idea