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 中捕获异常, 这时再操作事务机制可以回滚了.

同时也发现了这个也应该算是一个编码规范.记录.




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