spring transaction management Propagation.REQUIRES_NEW无效

Din*_*nka 3 spring transactions propagation required requires

我的服务类.

@Service
@Transactional(value = "transactionManager", readOnly = true, propagation = Propagation.REQUIRED)
public class DeviceServiceImpl{

@Transactional(readOnly = false)
public void blockAllDevices(){

    createSmsDeviceBlock();

}


public void createSmsDeviceBlock(){

    addToLogTables();

    smsService.sendSms();
}


@Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW)
public void addToLogTables(){
         try {
          //save object with DAO methods...
        } catch (Exception e) {
          throw new ServiceException(ServiceException.PROCESSING_FAILED, e.getMessage(), e);
        }
}
Run Code Online (Sandbox Code Playgroud)

}

从我的控制器,服务方法blockAllDevices()被调用.addToLogTables()方法被标记为Propergation.REQUIRED_NEW,但问题是addToLogTables()方法没有创建新事务并且现有事务正在使用.

我想要做的是,addToLogTables()方法上的事务应该在执行smsService.sendSms()方法之前提交.

我的问题在于,如果事务无法提交,则在方法addToLogTables()方法中,它不应该执行smsService.sendSms()方法.

Sot*_*lis 10

那不是Propagation.REQUIRES_NEW问题.这是@Transactional代理如何工作的问题.

当Spring代理您注释的bean时@Transactional,它基本上将其包装在代理对象中,并在打开事务后委托给它.当委托调用返回时,代理提交或回滚事务.

例如,你的bean是

@Autowired
private DeviceServiceImpl deviceService;
Run Code Online (Sandbox Code Playgroud)

Spring实际上是要注入一个包装器代理.

所以,当你这样做

deviceService.blockAllDevices();
Run Code Online (Sandbox Code Playgroud)

您正在调用具有事务行为的代理上的方法.但是,blockAllDevices()你在做

createSmsDeviceBlock();
Run Code Online (Sandbox Code Playgroud)

这实际上是

this.createSmsDeviceBlock();
Run Code Online (Sandbox Code Playgroud)

where this指的是实际对象,而不是代理,因此没有事务行为.

这在文档中进一步解释.

你必须重新设计你的设计.