---
title: "spring事务回滚"
author: "waainli"
date: "2012-11-13T16:36:37Z"
tags: []
categories: ["数据库"]
slug: "spring"
draft: false
lastmod: "2012-11-13T19:52:01Z"
id: 1724751
url: "https://jxyang.iteye.com/blog/1724751"
---

**spring的事务管理是类级别的，而不是方法级别的！**

 

不管在spring配置文件中如何设置spring的传播propagation级别，**同一个类的不同方法都在同一个事务中！！**

 

 

**代码说明一：**

 

代码一：

 

 

```
@Service
public class TestServiceImpl implements TestService {

	@Resource
	private TestDAO testDAO;

	/* (non-Javadoc)
	 * @see com.cums.pds.service.TestService#test3()
	 */
	@Override
	public void test3() {
		String SQL = "{  call TEST2 }";
		testDAO.bulkUpdateBySQL(SQL);
		testDAO.flush();
	}

	/* (non-Javadoc)
	 * @see com.cums.pds.service.TestService#test4()
	 */
	@Override
	public void test4() {
		test3();
		
		String sql = "insert into tbl_test(merchant_no) values(6)";
		testDAO.bulkUpdateBySQL(sql);
		testDAO.flush();
		throw new AppException("error..................");
	}
	
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext*.xml");
		TestService s = (TestService) ctx.getBean("testServiceImpl");
		s.test4();
	}

}
```

 

在上面的代码中，即使将propagation设置为"REQUIRES\_NEW"，test4方法中的回滚也引起了test3方法的回滚。

 

 

**代码说明二：**

代码二：

 

 

```
@Service
public class TestService2Impl implements TestService2 {

//	@Resource
//	private TestDAO testDAO;
	@Resource
	private TestDAO2 testDAO2;

	/* (non-Javadoc)
	 * @see com.cums.pds.service.TestService#test3()
	 */
	@Override
	public void test3() {
		String SQL = "{  call TEST2 }";
//		testDAO.bulkUpdateBySQL(SQL);
//		testDAO.flush();
		testDAO2.bulkUpdateBySQL(SQL);
		testDAO2.flush();
	}
}
```

 

代码三：

 

```
@Service
public class TestServiceImpl implements TestService {

	@Resource
	private TestDAO testDAO;
	@Resource
	private TestService2 testService2;

	/* (non-Javadoc)
	 * @see com.cums.pds.service.TestService#test4()
	 */
	@Override
	public void test4() {
		testService2.test3();
		
		String sql = "insert into tbl_test(merchant_no) values(6)";
		testDAO.bulkUpdateBySQL(sql);
		testDAO.flush();
		throw new AppException("error..................");
	}
	
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext*.xml");
		TestService s = (TestService) ctx.getBean("testServiceImpl");
		s.test4();
	}
}
```

 

这种情况下，如果propagation设置为"REQUIRES\_NEW"，testService的test4方法回滚，testService2的test3方法并不回滚。

如果propagation设置为"NESTED"，testService的test4方法回滚，testService2的test3方法也回滚。

 

 

要注意的是，是否使用同一个DAO类并不影响结果的。

把代码二中的testDAO2全部换成testDAO，不影响结果。

 

 

**代码说明三：**

 

代码四：

 

```
@Service
public class TestService2Impl implements TestService2 {

//	@Resource
//	private TestDAO testDAO;
	@Resource
	private TestDAO2 testDAO2;

	/* (non-Javadoc)
	 * @see com.cums.pds.service.TestService#test3()
	 */
	@Override
	public void test3() {
		String SQL = "{  call TEST2 }";
//		testDAO.bulkUpdateBySQL(SQL);
//		testDAO.flush();
		testDAO2.bulkUpdateBySQL(SQL);
		testDAO2.flush();
		throw new AppException("exception......");
	}
}
```

 

 

代码五：

 

```
@Service
public class TestServiceImpl implements TestService {

	@Resource
	private TestDAO testDAO;
	@Resource
	private TestService2 testService2;

	/* (non-Javadoc)
	 * @see com.cums.pds.service.TestService#test4()
	 */
	@Override
	public void test4() {
		String sql = "insert into tbl_test(merchant_no) values(6)";
		testDAO.bulkUpdateBySQL(sql);
		testDAO.flush();
		
		testService2.test3();
//		throw new AppException("error..................");
	}
	
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext*.xml");
		TestService s = (TestService) ctx.getBean("testServiceImpl");
		s.test4();
	}
}
```

 这种情况下，**如果propagation设置为"REQUIRES\_NEW"，testService2的test3方法回滚，testService的test4方法也回滚。**因为，**testService2的test3方法抛出异常，testService的test4方法并没有处理这个异常，因此，testService的test4方法也产生了异常。因此都回滚。此时，如果对testService2的test3方法使用try。。。catch。。。，则testService的test4方法正常提交，testService2的test3方法回滚。**

如果propagation设置为"NESTED"，testService2的test3方法回滚，testService的test4方法也回滚。

 

**REQUIRES\_NEW和NESTED的区别在于：**

外层事务异常回滚时，REQUIRES\_NEW内层事务不回滚，因为它与外层事务相对独立；

NESTED内层事务回滚，因为它是外层事务的一部分。

但是，如果内层事务回滚，则外层事务如果不捕捉异常都会回滚，捕捉异常后外层事务不回滚。

 

感谢这篇文章：

 

spring事务传播级别 写道

1： PROPAGATION\_REQUIRED  
加入当前正要执行的事务不在另外一个事务里，那么就起一个新的事务  
比如说，ServiceB.methodB的事务级别定义为PROPAGATION\_REQUIRED, 那么由于执行ServiceA.methodA的时候，ServiceA.methodA已经起了事务，这时调用ServiceB.methodB，ServiceB.methodB看到自己已经运行在ServiceA.methodA的事务内部，就不再起新的事务。而假如ServiceA.methodA运行的时候发现自己没有在事务中，它就会为自己分配一个事务。这样，在ServiceA.methodA或者在ServiceB.methodB内的任何地方出现异常，事务都会被回滚。  
2： PROPAGATION\_SUPPORTS  
如果当前在事务中，即以事务的形式运行，如果当前不在一个事务中，那么就以非事务的形式运行  
3： PROPAGATION\_MANDATORY  
必须在一个事务中运行。也就是说，只能被一个父事务调用。否则，就要抛出异常。  
4： PROPAGATION\_REQUIRES\_NEW  
比如我们设计ServiceA.methodA的事务级别为PROPAGATION\_REQUIRED，ServiceB.methodB的事务级别为PROPAGATION\_REQUIRES\_NEW，  
那么当执行到ServiceB.methodB的时候，ServiceA.methodA所在的事务就会挂起，ServiceB.methodB会起一个新的事务，等待ServiceB.methodB的事务完成以后，它才继续执行。它与PROPAGATION\_REQUIRED 的事务区别在于事务的回滚程度了。因为ServiceB.methodB是新起一个事务，那么就是存在  
两个不同的事务。如果ServiceB.methodB已经提交，那么ServiceA.methodA失败回滚，ServiceB.methodB是不会回滚的。如果ServiceB.methodB失败回滚，它抛出的异常被ServiceA.methodA捕获，ServiceA.methodA事务仍然可以提交。  
5： PROPAGATION\_NOT\_SUPPORTED  
当前不支持事务。比如ServiceA.methodA的事务级别是PROPAGATION\_REQUIRED ，而ServiceB.methodB的事务级别是PROPAGATION\_NOT\_SUPPORTED ，那么当执行到ServiceB.methodB时，ServiceA.methodA的事务挂起，而它以非事务的状态运行完，再继续ServiceA.methodA的事务。  
6： PROPAGATION\_NEVER  
不能在事务中运行。假设ServiceA.methodA的事务级别是PROPAGATION\_REQUIRED， 而ServiceB.methodB的事务级别是PROPAGATION\_NEVER ，那么ServiceB.methodB就要抛出异常了。  
7： PROPAGATION\_NESTED  
理解Nested的关键是savepoint。它与PROPAGATION\_REQUIRES\_NEW的区别是，PROPAGATION\_REQUIRES\_NEW另起一个事务，将会与它的父事务相互独立，而Nested的事务和它的父事务是相依的，它的提交是要等和它的父事务一块提交的。也就是说，如果父事务最后回滚，它也要回滚的。  
而Nested事务的好处是它有一个savepoint。  
class ServiceA {  
/*  
\* 事务属性配置为 PROPAGATION\_REQUIRED  
\*/  
void methodA() {  
try {  
// savepoint  
ServiceB.methodB(); //PROPAGATION\_NESTED 级别  
} catch (Exception e) {  
// 执行其它业务, 如 ServiceC.methodC();  
}  
}  
}  
也就是说ServiceB.methodB失败回滚，那么ServiceA.methodA也会回滚到savepoint点上，ServiceA.methodA可以选择另外一个分支，比如  
ServiceC.methodC，继续执行，来尝试完成自己的事务。但是这个事务并没有在EJB标准中定义。