spring事务的两种配置方式,不是很理解。先放到这里。

本文介绍了一种使用Spring框架进行事务管理的具体配置方案。通过AOP方式针对特定命名的方法(如insert*, update*等)实现自动化的事务管理,并对查询方法设置为只读以提升性能。

一、

<!-- 事务管理 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 -->
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
            <tx:method name="fileUpload" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
            <tx:method name="fileDownload" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
            <!-- select,count开头的方法,开启只读,提高数据库访问性能 -->
            <tx:method name="select*" read-only="true"/>
            <tx:method name="count*" read-only="true"/>
            <!-- 对其他方法 使用默认的事务管理 -->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 事务 aop 配置 -->
    <aop:config>
        <aop:pointcut id="serviceMethods" expression="(execution(* org.szfzx.siss.**.service.**.**.*(..)))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>

    <!-- 配置使Spring采用CGLIB代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 启用对事务注解的支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>


二、

<!-- 事务 aop 配置 -->
    <aop:config>
        <aop:pointcut id="serviceMethods1" expression="execution(* org.szfzx.siss.**.service.**.**.update*(..))"/>
        <aop:pointcut id="serviceMethods2" expression="execution(* org.szfzx.siss.**.service.**.**.insert*(..))"/>
        <aop:pointcut id="serviceMethods3" expression="execution(* org.szfzx.siss.**.service.**.**.delete*(..))"/>
        <aop:pointcut id="serviceMethods4" expression="execution(* org.szfzx.siss.**.service.**.**.fileUpload(..))"/>
        <aop:pointcut id="serviceMethods5" expression="execution(* org.szfzx.siss.**.service.**.**.fileDownload(..))"/>

        <aop:advisor pointcut-ref="serviceMethods1" advice-ref="txAdvice"/>
        <aop:advisor pointcut-ref="serviceMethods2" advice-ref="txAdvice"/>
        <aop:advisor pointcut-ref="serviceMethods3" advice-ref="txAdvice"/>
        <aop:advisor pointcut-ref="serviceMethods4" advice-ref="txAdvice"/>
        <aop:advisor pointcut-ref="serviceMethods5" advice-ref="txAdvice"/>
    </aop:config>
    
    <!-- 事务管理 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="*" rollback-for="java.lang.Exception" />  
       </tx:attributes>  
    </tx:advice>  

 

转载于:https://www.cnblogs.com/airduce/p/8625600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值