当我尝试捕获DataIntegrityViolationException
并将其转换为我的自定义异常时,catch块不会执行。Spring TransactionTemplate不处理异常
正如我使用的代码从一个这样的示例: guide
结果,ServicePointRepository
抛出DataIntegrityViolationException
是在我的控制器层处理,并且在运行时代码似乎是避免catch块。
我在做什么错?
这是PersistenceService代码:
@Autowired
private ServicePointRepository servicePointRepository;
@Autowired
private BusinessExceptionFactory businessExceptionFactory;
@Autowired
private TransactionTemplate transactionTemplate;
@Override
public String save(final ServicePointDTO servicePointDTO) {
final ServicePointEntity servicePointEntity = mapToEntity(servicePointDTO);
return transactionTemplate.execute(status -> {
try {
return servicePointRepository.save(servicePointEntity).getId().toString();
} catch (DataIntegrityViolationException e) {
throw businessExceptionFactory.createBusinessException(AlreadyExistException.class, CommonError.ALREADY_EXIST);
}
});
}