spring全注解事务管理中怎么手动回滚事物

spring全注解事务管理中怎么手动回滚事物,第1张

spring事物配置:
配置事务管理器
启用基于注解的事务管理
通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务,proxy-target-class="true"使用CGLIB代理和@AspectJ自动代理支持
Spring的事物是捕获异常来进行事物回滚,有时候我们需要根据业务逻辑来判断是否需要事物回滚。这时候我们就用到手动事物回滚了

事务边界是要看你Spring配置的事务切面,从你说的情况来看你应该把切面设置在dao层了,如果希望在service层自动回滚事务就要把切面指定在service层。

<aop:pointcut id="serviceOperation" expression="execution( comxiancardwxservice())" />  

这个你能看到我指定的切面在comxiancardwxservice这个包下的所有类,也就是service层。这样当调用insert  update等开头的方法是SPRING会自动启用事务。

   <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
            <tx:method name="insert" propagation="REQUIRED"></tx:method>
            <tx:method name="update" propagation="REQUIRED"></tx:method>
            <tx:method name="edit" propagation="REQUIRED"></tx:method>
            <tx:method name="save" propagation="REQUIRED"></tx:method>
            <tx:method name="add" propagation="REQUIRED"></tx:method>
            <tx:method name="new" propagation="REQUIRED"></tx:method>
            <tx:method name="set" propagation="REQUIRED"></tx:method>
            <tx:method name="remove" propagation="REQUIRED"></tx:method>
            <tx:method name="delete" propagation="REQUIRED"></tx:method>
            <tx:method name="change" propagation="REQUIRED"></tx:method>
            <tx:method name="get" propagation="REQUIRED" read-only="true"></tx:method>
            <tx:method name="find" propagation="REQUIRED" read-only="true"></tx:method>
            <tx:method name="load" propagation="REQUIRED" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>
        <aop:config> 
          <aop:pointcut id="serviceOperation" expression="execution( comxiancardwxservice())" /> 
          <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
        </aop:config>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/10431076.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-08
下一篇 2023-05-08

发表评论

登录后才能评论

评论列表(0条)

保存