@Transactional事务传播级别,求赐教!

public class AController{
   @Autowired
    Aservice aService;

   @Autowired
    Bservice bService;

    public String test(){
        try{
            aService.a();
            bService.b();
        }catch(Exception e){
            e.printStackTrace();
             logger.error("操作错误");
        }
    }
}


// Aservice
public interface AserviceImpl implements Aservice{
    
    @Transactional(rollbackFor = Exception.class)
    public String a(){
        dao.insert();
    }
}


// Bservice
public interface BserviceImpl implements Bservice{
    
    @Transactional(rollbackFor = Exception.class)
    public String b(){
        dao.insert();
    }
}

 

Acontroller在一个方法里分别调用A、B两个service里的a、b方法。

用什么样的事务传播级别才能使两个方法任何一个方法出现异常,两个方法都进行回滚!
请大神赐教,十分感谢!

	
public class AController{
	
   @Autowired
	
    Aservice aService;
	
 
	
   @Autowired
	
    Bservice bService;
	
   @Transactional(rollbackFor = Exception.class)
	
    public String test(){
	
        try{
	
            aService.a();
	
            bService.b();
	
        }catch(Exception e){
	
            e.printStackTrace();
	
             logger.error("操作错误");
	
        }
	
    }
	
}
	
 
	
 
	
// Aservice
	
public interface AserviceImpl implements Aservice{
	
    
	
    @Transactional(rollbackFor = Exception.class)
	
    public String a(){
	
        dao.insert();
	
    }
	
}
	
 
	
 
	
// Bservice
	
public interface BserviceImpl implements Bservice{
	
    
	
    @Transactional(rollbackFor = Exception.class)
	
    public String b(){
	
        dao.insert();
	
    }
	
}

这样就可以,默认级别就可以

我试过了不可以,所以才来请教的。😂

这个添加了吗

 

这个方法可行,但是要去掉try catc。 如果不去掉的话,还有办法吗?

public class AController{
	
   @Autowired
	
    Aservice aService;
	
 
	
   @Autowired
	
    Bservice bService;
	
   @Transactional(rollbackFor = Exception.class)
	
    public String test(){
	
        try{
	
            aService.a();
	
            bService.b();
	
        }catch(Exception e){
	
            e.printStackTrace();
	
             logger.error("操作错误");
	
        }
	
    }
	
}
	
 
	
 
	
// Aservice
	
public interface AserviceImpl implements Aservice{
	
    
	
    public String a(){
	
        dao.insert();
	
    }
	
}
	
 
	
 
	
// Bservice
	
public interface BserviceImpl implements Bservice{
	
    
	
	
    public String b(){
	
        dao.insert();
	
    }
	
}

这个试一下就可以了

去掉这两个