使用SpringS声明式的开启事务

1. 导入事务依赖

       xmlns:tx="http://www.springframework.org/schema/tx"
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd

2. 声明事务

<!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置aop织入事务-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.yd.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

这里需要注意,事务的使用可能会失效,其主要原因可能由好多种,这里举两个例子:

1. 数据库存储引擎不是innodb;

2. 方法B开启了事务,但是方法A没有开启事务,在方法A中调用方法B时,就会出现事务失效的问题。

原文地址:https://www.cnblogs.com/chao666/p/12880538.html