1. 程式人生 > >Spring 事務配置實戰(一):過濾無需事務處理的查詢之類操作

Spring 事務配置實戰(一):過濾無需事務處理的查詢之類操作

log pla ssi pan spl tail gif aop img

技術分享
<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
                       rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
                       rollback
-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> </tx:attributes> </tx:advice>
tx:advice

各標簽配置可參考:tx:advice詳解

Tips:

tx:method中name標明的方法為transactionManager管理的需要應用事務處理方法。

不過,不符name的方法,也會應用到事務。so,日誌會有Don‘t need create transactionManager……提示。

去除上則消息,需要在AOP註入配置中過濾掉,如查詢。

具體參考以下配置:

技術分享
<aop:config>
        <aop:pointcut id="pc" expression="(execution(* com.*.service..*(..))) 
            and !(execution(* com.*.service..*.check*(..)))
            and 
!(execution(* com.*.service..*.find*(..))) and !(execution(* com.*.service..*.list*(..))) and !(execution(* com.*.service..*.get*(..)))" /> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" /> </aop:config>
aop:config

各標簽配置可參考:aop:config詳解

去除aop註入的service包及子包中,以find,get,list,check開頭的方法。

如此,查詢操作即可不需用到事務處理,也就沒了Don‘t need create transactionManager……的提示。

Spring 事務配置實戰(一):過濾無需事務處理的查詢之類操作