一@Transactional
方法调用到另一2层的方法,其也存在于@Transactional
的方法,但同时被调用的方法的一个得到例外它的事务应该被回滚,其不会发生
-----The Main Transactional method-------------
@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = RestException.class)
public BaseDto createFPSAndUser(FpsStoreDto fpsStoreDto){
log.info("<--Starts FPSStoreService .createFPSAndUser-->"+fpsStoreDto);
BaseDto baseDto = new BaseDto();
try {
UserDetailDto userDetailDto = fpsStoreDto.getUserDetailDto();
userDetailDto.setCreatedBy(fpsStoreDto.getCreatedBy());
baseDto = createFPSStore(fpsStoreDto);
if(baseDto.getStatusCode() != 0){
throw new RestException(ErrorCodeDescription.getDescription(baseDto.getStatusCode()));
}
userDetailDto.setFpsStore(null);
baseDto = userDetailService.createUserDetail(userDetailDto);
if(baseDto.getStatusCode() != 0){
throw new RestException(ErrorCodeDescription.getDescription(baseDto.getStatusCode()));
}
FPSStore fpsStore = fpsStoreRepository.findByCode(fpsStoreDto.getCode());
UserDetail userDetail = userDetailRepository.findByUserId(userDetailDto.getUserId());
userDetail.setFpsStore(fpsStore);
userDetailRepository.save(userDetail);
baseDto.setStatusCode(0);
} catch(RestException restException){
log.info("RestException -:", restException);
restException.printStackTrace();
baseDto.setStatusCode(baseDto.getStatusCode());
} catch (Exception exception) {
log.info("Exception -:",exception);
exception.printStackTrace();
baseDto.setStatusCode(ErrorCodeDescription.ERROR_GENERIC.getErrorCode());
}
log.info("<--Ends FPSStoreService .createFPSAndUser-->"+baseDto);
return baseDto;
}
------------------Called method 1st-----------
@Transactional(propagation = Propagation.REQUIRED)
public BaseDto createFPSStore(FpsStoreDto fpsStoreDto) {
_________________________
__________________________
________________________
return baseDto;
}
------------------------2nd Transactional method-----
@Transactional(propagation = Propagation.REQUIRED)
public BaseDto createUserDetail(UserDetailDto userDetaildto) {
_______________
_______________
_______________
return baseDto
}
答案 0 :(得分:2)
您已设置rollbackFor=RestException.class
,但您的代码会捕获该异常并且不会重新抛出它。从Spring的角度来看,RestException
从未被方法抛出,并且没有理由回滚事务。
如果要进行回滚,则需要在catch块的末尾执行throw restException;
。
答案 1 :(得分:1)
你告诉Spring只在
时回滚事务rollbackFor = RestException.class
但如果你抓住了例外
catch(RestException restException){
Spring永远不会注意到抛出异常。您需要移除捕获阻塞(两者都有)或您可以在捕获结束时抛出异常
catch(RestException restException){
log.info("RestException -:", restException);
restException.printStackTrace();
baseDto.setStatusCode(baseDto.getStatusCode());
throw restException;
}
答案 2 :(得分:0)
@Transactional
告诉容器(spring)处理带注释的方法调用的事务管理。
`
rollbackFor=RestException.class
内部人员不会继承配置,并且在抛出休息异常时不会触发回滚