Spring事务注解@Transactional回滚问题

Spring配置文件,声明事务时,如果rollback-for属性没有指定异常或者默认不写;经测试事务只回滚运行时异常(RuntimeException)和错误(Error)。

<!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 注解方式配置事物 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 不添加rollback-for属性,事务只回滚运行时异常(RuntimeException)和错误(Error) -->
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>

            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />

        </tx:attributes>
    </tx:advice>
    <!-- Spring aop事务管理 -->
    <aop:config>
        <aop:pointcut id="transactionPointcut"
            expression="execution(* com.lwj.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut"
            advice-ref="transactionAdvice" />
    </aop:config>

Spring API地址:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html

附java异常类Throwable图:

原文地址:https://www.cnblogs.com/lyxy/p/5383197.html