springMVC事务回滚

    xiaoxiao2021-12-14  17

    一个service方法就是一个事务

    <!-- 定义事务传播属性 -->

       <tx:advice id="txAdvice" transaction-manager="transactionManager">

          <tx:attributes>

             <tx:method name="query*" propagation="NOT_SUPPORTED"

                read-only="true" />

             <tx:method name="*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>

             <!-- <tx:method name="*"propagation="REQUIRED" />-->

          </tx:attributes>

       </tx:advice>

     

    name 是配的dao层的方法

     

    roolback-for 需要回滚的异常名称

     

    如果不写roolback-for,spring默认RuntimeException、UncheckedException及error才会回滚;

    如果异常被try{ }catch(){}了,事务就不会回滚了;如果要想让事务回滚,则必须在catch里面再往外抛异常try{ }catch(){throw newException}

    转载请注明原文地址: https://ju.6miu.com/read-965377.html

    最新回复(0)