(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