事務(wù)的傳播特性:
一張圖很清楚的描述事務(wù)的傳播特性:
- Required:當(dāng)前有事務(wù)時使用當(dāng)前的事務(wù)身诺,當(dāng)前沒有事務(wù)時創(chuàng)建新的事務(wù)
實(shí)例
對不同的操作類型配置不同的傳播類型
<aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"
no-rollback-for="com.carease.cms.common.ex.SimpleNoRollbackException"/>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="modify*" 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:method name="upload*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="do*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="noSupport*" propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
read-only=true的操作表示可以進(jìn)行讀操作不可以進(jìn)行寫操作抛虫;read-only=false表示可以進(jìn)行讀寫操作。
將事務(wù)控制在service層:
<!--把事務(wù)控制在Service層-->
<aop:config>
<aop:pointcut id="pc" expression="execution(public * com.carease.cms.eps.biz.*.service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>