Understanding of declarative transaction management in spring

The meaning of declarative transaction management: the name of the method in the service will determine whether the method uses transactions.

Configuration of transaction in applicationContext.xml

<!--事务管理的 增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--被管理的 方法, 需要事务的方法-->
            <!--
                isolation: 事务的隔离级别  default数据库缺省的隔离级别
                read-only: 确定是否是 只读事务,
                propagation: 事务的传播级别,  常用required , supports   只读事务选support 需要事务就required
                rollback-for: 异常的类型,回滚
                no-rollback-for: 异常类型,进行提交
            -->
            <!--声明式的事务管理 service中方法起什么名字就将决定是否使用事务-->
            <tx:method name="transfer*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
            <tx:method name="insert*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
            <tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
            <tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />

            <tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
            <tx:method name="select*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

    <!--增强 和 事务管理对象切入-->
    <aop:config>
        <aop:pointcut id="txPointCut"
                expression="execution(* com.chinasoft.spring.service.impl.AccountServiceImpl.*(..))"
        ></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" ></aop:advisor>
    </aop:config>

In the applicationContext.xml file, the methods starting with transfer, insert, delete, and remove are first defined to enable transaction

And the methods at the beginning of find and select do not use transactions

<tx:method name="transfer*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="insert*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="delete*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />
<tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" />

<tx:method name="find*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />
<tx:method name="select*" isolation="DEFAULT" read-only="true" propagation="SUPPORTS" />

The methods in the service interface class are as follows

public interface AccountService {
    
    

    // 删除 用户信息
    void deleteAccount(Integer id);

    // 进行银行的账号之间的转账
    void transfer(String from, String to, float money) throws Exception;

    List<Account> findAll();
}

The deleteAccount and transfer methods will use transaction management

The findAll method does not use transaction management.

Summary: Declarative transaction management first defines which naming methods use transactions, and names the methods according to whether to enable transactions.

Guess you like

Origin blog.csdn.net/Hambur_/article/details/110517392