Configure declarative transactions

1. Enable the configuration


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/beans/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/beans/spring-tx.xsd ">
    <! -- Configure transaction manager -->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>
Copy the code

2. Configure transaction attributes

<! -- 2. Configure transaction properties -->
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add" propagation="REQUIRED"/><! -- Select the method to be processed -->
        <tx:method name="select" read-only="true"/>
        <tx:method name="delete"/>
    </tx:attributes>
</tx:advice>
Copy the code

1.PROPAGATION_REQUIRED(default implementation) : If there is no transaction, create a new one, and add the current one if there is

2.PROPAGATION_SUPPORTS: The current transaction is supported, and non-transactionally if there is no transaction currently executed

3.PROPAGATION_MANDATORY: The current transaction is used, and an exception is thrown if there is none

4.PROPAGATION__REQUIRES_NEW: Create a transaction, and suspend the current transaction if one exists

5. Execute in non-transactional mode, PROPAGATION_NOT_SUPPORIED, and suspend the current transaction if one exists

6. Execute non-transactionally, throw an exception if a transaction currently exists

7.PROPAGATION_NESTED: Execute a nested transaction if a transaction exists. If no transaction exists, execute 1

Aop weave

<aop:config>
    <aop:pointcut id="txPointcut" expression="execution(* mapper.*.*(..) )"/>
    <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="txPointcut"></aop:advisor>
</aop:config>
Copy the code