1.Required: 如果有事务运行,则当前方法在这个事务中运行;否则,就启动一个新事务 (外部事务统一管理)
//B抛出异常,A,B,C都要回滚
@Transactional(propagation =Propagation.REQUIRED)
public void methodParentA(){
try{
methodChildB();
}catch (Exception e){
e.printStackTrace();
}
methodChildC();
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildB(){
//todo
throw new RuntimeException();
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildC(){
//tod
}
2.Required_New: 当前的方法必须启动一个新的事务,并在自己的事务里运行;外面事务挂起
(1)A:Requred, B: Required_New ,C:required , B抛出异常,会导致
// B抛出异常,B回滚;A接收到异常,会导致C回滚
@Transactional(propagation =Propagation.REQUIRED)
public void methodParentA(){
methodChildB();
methodChildC();
}
@Transactional(propagation =Propagation.REQUIRES_NEW)
public void methodChildB(){
//todo
int a = 1/0;
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildC(){
//tod
}
(2)内层requred_new异常,不影响外层异常
//B抛出异常,B回滚;A捕获了此异常,则C不影响,不回滚
@Transactional(propagation =Propagation.REQUIRED)
public void methodParentA(){
try{
methodChildB();
}catch (Exception e){
e.printStackTrace();
}
methodChildC();
//int a = 1/0;
}
@Transactional(propagation =Propagation.REQUIRES_NEW)
public void methodChildB(){
//todo
int a = 1/0;
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildC(){
//tod
}
(3)外层事务异常,不影响内层独立事务
//A抛出异常,C回滚;B是一个独立的新事务,不受影响
@Transactional(propagation =Propagation.REQUIRED)
public void methodParentA(){
try{
methodChildB();
}catch (Exception e){
e.printStackTrace();
}
methodChildC();
int a = 1/0;
}
@Transactional(propagation =Propagation.REQUIRES_NEW)
public void methodChildB(){
//todo
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildC(){
//tod
}
3.Support: 如果有事务,就在事务中运行;否则就不在事务中运行
4.Not Support:当前的方法不应该运行在事务中,如果有事务,就挂起 (当做普通方法使用) — 与 Nerver 对应
5.MANDATORY (强制): 当前方法必须运行在事务中,如果没有事务,就抛出异常
6.Nerver: 当前的方法不应该运行在事务中,如果有事务,就抛出异常
7.NESTED: 如果有事务在运行,当前方法就应该在这个事务的嵌套事务中运行,否则就启动一个新的事务
(1)Nested嵌套事务抛出异常,但是捕获了,则不影响外层事务
//B抛出异常,A捕获了异常,则不影响C
@Transactional(propagation =Propagation.REQUIRED)
public void methodParentA(){
try{
methodChildB();
}catch (Exception e){
e.printStackTrace();
}
methodChildC();
//int a = 1/0;
}
@Transactional(propagation =Propagation.NESTED)
public void methodChildB(){
//todo
int a = 1/0;
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildC(){
//tod
}
(2)外层事务异常,导致Nested嵌套事务回滚
//A 方法抛出了异常,因为B是A的嵌套事务,则B,C一起回滚
@Transactional(propagation =Propagation.REQUIRED)
public void methodParentA(){
try{
methodChildB();
}catch (Exception e){
e.printStackTrace();
}
methodChildC();
int a = 1/0;
}
@Transactional(propagation =Propagation.NESTED)
public void methodChildB(){
//todo
//int a = 1/0;
}
@Transactional(propagation =Propagation.REQUIRED)
public void methodChildC(){
//tod
}