spring事務失效情況分析


詳見:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt113

<!--[if !supportLists]-->一、<!--[endif]-->聲明式事務和注解事務並存的情況下,事務失效。

 

該情況常見配置如下:

 

<!--事務聲明-->

<bean name="transactionManager"

    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource"></property>

    </bean>

<!--聲明式事務 –->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

       <tx:attributes>

           <tx:method name="insert*" 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="update*" 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="delete*" propagation="REQUIRED" read-only="false"

              rollback-for="java.lang.Exception" />

           <tx:method name="find*" propagation="SUPPORTS" />

           <tx:method name="get*" propagation="SUPPORTS" />

           <tx:method name="select*" propagation="SUPPORTS" />

       </tx:attributes>

    </tx:advice>

   

    <aop:config>

       <aop:pointcut id="pc"

           expression="execution(* com.suning.sop.*.service.*(..))" />

       <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />

    </aop:config>

 

<!—注解式事務 –->

<tx:annotation-driven

proxy-target-class="true"

transaction-manager="transactionManager"/>

 

 

 

解決方法:聲明優先級,order,值越大優先級越低

將上例中,<aop:config>節點修改成如下所示

    <aop:config>

       <aop:pointcut id="pc"

           expression="execution(* com.suning.sop.*.service.*(..))" />

       <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" order="1"/>

    </aop:config>

 

<!--[if !supportLists]-->二、<!--[endif]-->Controller,Service層和Dao層全部使用注解生成bena,Service層方法也使用注解聲明事,這種情況由於 @Controller 會把 關聯的@Servie自動實例化,而此時Service層中聲明的事務並沒有被實例化,導致事務失效。

解決方法:

 

<context:component-scan base-package="com.comtop"  use-default-filters="false" >

        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>    

</context:component-scan>    

  

<context:component-scan base-package="com.comtop">    

        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>    

</context:component-scan>   

Include:把@Controller關聯的@Service自動實例化

exclude:屏蔽@Service自動實例化

<!--[if !supportLists]-->三、<!--[endif]-->三層都使用注解,事務也使用注解聲明。事務失效

Spring 會先加載聲明在xml文件中的Bean

其次是加載<context:component-scan>標簽聲明的包中,

聲明了@Controller,@Service,@Component,@Re @Repository的類

最后是 @Transactional 標記,但上一步處理component-scan 聲明的包時,類包含的@Transactional標記沒有被處理,所以事務控制失效。

 

解決方法:

 

Service 層的類使用xml文件聲明bean,但是事務控制仍然使用@Transactional注解來聲明,事務控制才能生效


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM