Spring actively throws Exception without rolling back

Today, my colleague tested the transaction rollback and found that the transaction worked, but the data will not be rolled back after actively throwing Exception.

After checking the configuration, I didn't find any problems. Configuration:

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="query" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

After checking the information, I found that spring aop will only roll back the exceptions of RuntimeException and its subclasses by default. Exception will not be rolled back, and it will be rolled back normally after modification.

Solution:

1. Change the actively thrown Exception to the exception of RuntimeException or its subclasses.

2. Add rollback-for="Exception" configuration to <tx:method name="do*" propagation="REQUIRED" /> in the configuration.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326915678&siteId=291194637