spring 声明式事务传播特性详解(包括异常回滚举例)

1:Service接口实现

public interface IProcessBusiService {
      //事务1
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void submit(ProcessResult result) throws Exception;
2、public interface IBatchMigrationHistoryTaskService {


//事务2
@Transactional(readOnly=false,propagation = Propagation.REQUIRES_NEW)
public void updateBatStatus(EcdsBatchRunning batch) throws Exception;
//事务3
@Transactional(readOnly=false,propagation = Propagation.REQUIRED)
public void updateSyspara(SysParameter para) throws Exception;
//事务4
@Transactional(readOnly=false,propagation = Propagation.REQUIRED)
public void updateSysparaBank(SysParaBank para) throws Exception;




}


3:Impl实现类
@Service("ProcessQueryTimeTaskService")
public class ProcessQueryTimeTaskServiceImpl implements IProcessBusiService {


@Override
public void submit(ProcessResult result) throws Exception {
  runLoaningToFund(result);
}


public void runLoaningToFund(ProcessResult result1){
logger.info("***********  垫资未处理自动转为普通融资重处理开始 ***********");




EcdsBatchRunning batchRunning = null;
try {
  //第一个
batchRunning=(EcdsBatchRunning) commonQueryService.select(EcdsBatchRunning.class, BatchTypeStatus.BAT_18);
batchRunning.setRmrk("890");
batchMigrationHistoryTaskService.updateBatStatus(batchRunning);
//第二个
SysParameter para =(SysParameter) commonQueryService.select(SysParameter.class,18l );
para.setParaValue("119");
batchMigrationHistoryTaskService.updateSyspara(para);


//第三个
SysParaBank parabank =(SysParaBank) commonQueryService.select(SysParaBank.class,137l );
para.setParaValue("20");
batchMigrationHistoryTaskService.updateSysparaBank(parabank);


}catch (Exception e) {
e.printStackTrace();
}
}

总结:1:如果没有添加@Transactional事务说明,sql语句不会执行

2:如果只有事务1的时候,执行到第三个的时假如报异常,会回滚之前两个

               3:假如只有事务1的时候,在第二个和第三个之间出现int a = 10/0这种异常,那么第一个和第二个不会进行回滚操作。

4:存在事务1、2、3和4四个事务时,执行到第三个的时候,第一个不会回滚,第二个会回滚。


 


如果只有事务1的时候,执行到第三个的时假如报异常,会回滚之前两个


3:、下面图片是Spring 实战(第3版)给出的事务传播特性的截图

spring 声明式事务传播特性详解(包括异常回滚举例)_第1张图片

      @Transactional(readOnly=false,propagation = Propagation.REQUIRES_NEW)
public void updateBatStatus(EcdsBatchRunning batch) throws Exception;

@Transactional(readOnly=false,propagation = Propagation.REQUIRED)
public void updateSyspara(SysParameter para) throws Exception;

@Transactional(readOnly=false,propagation = Propagation.REQUIRED)
public void updateSysparaBank(SysParaBank para) throws Exception;

}

你可能感兴趣的:(Spring,java)