spring事物不起做用

 

一、首先使用以下代碼 確認你的bean 是代理對象嗎?spring

必須是Spring定義(經過XML或註解定義均可以)的Bean才接受事務。express

直接new出來的對象添加事務是不起做用的。mvc

能夠經過如下方式判斷是不是代理對象:函數

 

AopUtils.isAopProxy(Object object).net

AopUtils.isCglibProxy(Object object) //cglib代理

AopUtils.isJdkDynamicProxy(Object object) //jdk動態代理component

 

 二、入口函數必須是public,不然事務不起做用。這一點由Spring的AOP特性決定的。對象

 

 三、切入點配置錯誤。事務

<!-- 使用annotation註解方式配置事務 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>get

<!--使用切面方式配置事務-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="Java.lang.Exception" /> 
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.tyyd..*Service.do*(..))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
</aop:config> 

 

四、若是你使用了springmvc,多是context:component-scan重複掃描引發的:

 

五、如使用MySQL且引擎是MyISAM形成的(由於不支持事務),改爲InnoDB便可。

 

六、查看方法寫對沒有,不然攔截不到。

相關文章
相關標籤/搜索