spring事务回滚

(1)核心

          只有抛出RuntimeException才会回滚,其他情况都不会


(2)基本代码

                1.spring事务配置

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="123" />
	</bean>

	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="select*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRES_NEW"
				rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>

	<aop:config proxy-target-class="false">
		<aop:advisor pointcut="execution(* com.mycompany.app.service.*.*(..))"
			advice-ref="txAdvice" />
	</aop:config>

                2.service层

public class PersonService {
	@Autowired
	private PersonDAO personDAO;
	
	public void insertBatch(List<Person> list){
		personDAO.insertBatch(list);
	}
}

                3.sqlMap

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="person">
	<!-- 使用别名可以避免每次都输入类路径全称 -->
	<typeAlias alias="Person" type="com.mycompany.app.dataObject.Person"/>
	
	<insert id="insertPerson" parameterClass="Person">
         insert into person(name,age,version)
		 values(#name#,#age#,#version#)
		 <selectKey resultClass="java.lang.Integer" keyProperty="id">  
        	select last_insert_id() as id from person limit 1  
    	</selectKey> 
	</insert>
</sqlMap>


(3)不抛异常DAO

public class PersonDAOImpl extends SqlMapClientDaoSupport implements  PersonDAO 
{	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void insertBatch(final List<Person> list){
	         getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
	            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
	                executor.startBatch();
	                for (Person p:list) {
	                    executor.insert("person.insertPerson", p);
	                }
	                return executor.executeBatch();
	            }
	        });
	}
}
        测试:
public class PersonServiceTest 
{
	private PersonService personService;

	@Before
	public void before()
	{
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		personService = (PersonService) context.getBean("personService");
	}
	
	@Test
	public void testInsertBatch() 
		List<Person> persons = new ArrayList<Person>();
		for (int i=-5; i<10; i++) {
			Person person = new Person();
			person.setName("cc"+i);
			person.setAge(i);
			person.setVersion(1);
			persons.add(person);
		}
		personService.insertBatch(persons);
	}
}
             结果:成功插入


(4)抛CheckedException异常DAO

public class PersonDAOImpl extends SqlMapClientDaoSupport implements  PersonDAO 
{	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void insertBatch(final List<Person> list){
	         getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
	            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
	                executor.startBatch();
	                for (Person p:list) {
	                	if (p.getAge()==0) {
	                		try {
								InputStream inputStream = new FileInputStream(new File("xx"));
					} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
	                	}
	                    executor.insert("person.insertPerson", p);
	                }
	                return executor.executeBatch();
	            }
	        });
	}
}
         测试:

public class PersonServiceTest 
{
	private PersonService personService;

	@Before
	public void before()
	{
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		personService = (PersonService) context.getBean("personService");
	}
	
	@Test
	public void testInsertBatch() 
		List<Person> persons = new ArrayList<Person>();
		for (int i=-5; i<10; i++) {
			Person person = new Person();
			person.setName("cc"+i);
			person.setAge(i);
			person.setVersion(1);
			persons.add(person);
		}
		personService.insertBatch(persons);
	}
}
            结果仍然成功插入


(5)抛RuntimeException异常DAO

public class PersonDAOImpl extends SqlMapClientDaoSupport implements  PersonDAO 
{	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void insertBatch(final List<Person> list){
	         getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
	            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
	                executor.startBatch();
	                for (Person p:list) {
	                	if (p.getAge()==0) {
	                		try {
								InputStream inputStream = new FileInputStream(new File("xx"));
							} catch (FileNotFoundException e) {
								e.printStackTrace();
								throw new RuntimeException("divide zero");
							}
	                	}
	                    executor.insert("person.insertPerson", p);
	                }
	                return executor.executeBatch();
	            }
	        });
	}
}
              抛出RuntimeException,事务回滚,一条数据也没有插入

(6)总结

               如果在Service中遇到异常需要回滚,则一定要手动抛出RuntimeException


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值