spring-控制事物回滚

来源:互联网 发布:裤子 知乎 编辑:程序博客网 时间:2024/05/16 01:39

   点击打开链接

applicationContext.xml

 

Xml代码  收藏代码
  1. <!-- 
  2.         spring事务管理 方法一-->  
  3.     <bean id="transactionManager"  
  4.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  5.         <property name="dataSource" ref="dataSource" />  
  6.     </bean>  
  7.     <aop:config>  
  8.         <aop:pointcut id="serviceOperation" expression="execution(*  
  9.         com.*.service.*.*(..))" />  
  10.         <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />  
  11.     </aop:config>  
  12.   
  13.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  14.         <tx:attributes>  
  15.             <tx:method name="insert*" rollback-for="Exception" />  
  16.             <tx:method name="save*" rollback-for="Exception" />  
  17.             <tx:method name="update*" rollback-for="Exception" />  
  18.             <tx:method name="delete*" rollback-for="Exception" />  
  19.             <tx:method name="*" read-only="true" rollback-for="Exception" />  
  20.         </tx:attributes>  
  21.     </tx:advice>  

 

主要java代码:

 

Java代码  收藏代码
  1. public void insertAccounts(List<Account> accountList) {  
  2.   
  3.         for (int i = 0; i < accountList.size(); i++) {  
  4.             System.out.println(""+i);  
  5.             if(i==2){  
  6.                 //注释到就可以全部插入,否则spring的事物会让他们全部不插入,ACID  
  7.                 int s=2/0;//抛出异常  
  8.             }  
  9.             this.accountDao.insertAccount(accountList.get(i));  
  10.         }  
  11.     }  

 

 

  另外,附件也上传了一份jdbc的事物源代码,这个是复制自别人的的
0 0
原创粉丝点击