spring 事務處理常規記錄

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven />
    
    <aop:config >
     	<aop:advisor pointcut="execution(* com.abc.ccb.service.*.*(..))"  advice-ref="txAdvice" />
    </aop:config> 
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
   		<tx:attributes>
		    <tx:method name="new*" propagation="REQUIRED"/>
		    <tx:method name="edit*" propagation="REQUIRED"/>
		    <tx:method name="del*" propagation="REQUIRED"/>
		    <tx:method name="insert*" propagation="REQUIRED"/>
		    <tx:method name="save*" propagation="REQUIRED"/>
		    <tx:method name="*TX" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
		    <!-- <tx:method name="*" read-only="true" />  -->
    	</tx:attributes>
    </tx:advice>

以上是 applicationContext.xml  配置 事務和AOP。 (ORM是 ibatis)

執行的控制範圍在 com.abc.ccb.service 包裏的任意方法 . 感謝 http://blog.csdn.net/kkdelta/article/details/7441829 這篇博客提供的參考.


過程中出現不回滾一個小問題.

在service 中的一個 *TX方法中 我對數據庫操作語句進行的try catch 異常捕捉, 製造事務異常執行時,發現事務沒回滾.

然後解決方法:將service中異常向外拋throws ,action 中捕獲異常, 這時再操作事務機制可以回滾了.

同時也發現了這個也應該算是一個編碼規範.記錄.




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章