Wenn ich versuche, DataIntegrityViolationException
abzufangen und es in meine benutzerdefinierte Ausnahme konvertieren, wird catch-Block nicht ausgeführt.Spring TransactionTemplate behandelt keine Ausnahmen
Als Beispiel I-Code von benutzten: guide
Als Ergebnis wirft ServicePointRepository
DataIntegrityViolationException
, die an meiner Controller-Schicht behandelt wird, und der Code während der Laufzeit scheint catch-Block zu vermeiden.
Was mache ich falsch?
Dies ist der Code von 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);
}
});
}