如果发生任何错误,我想创建一个事务方法来删除数据库中的插入。
我的测试:我称之为,checkNamespaceAndService.getNamespace 是成功的,所以可以,但是 deploiment.setGitCommit 由于验证原因失败。所以抛出异常。
但在数据库中,保留了第一种方法的结果。
@Transactional(rollbackFor=Exception.class)
public HistoriqueDeploiement saveHistoriqueDeploiementMethod(HistoriqueDeploiementReadingDTO historiqueDeploiementReadingDTO) throws InternalServerErrorException {
try {
Namespace namespace = checkNamespaceAndService.getNamespace(historiqueDeploiementReadingDTO.getNamespace());
Service service = checkNamespaceAndService.getServices(historiqueDeploiementReadingDTO.getService());
HistoriqueDeploiement deploiment = new HistoriqueDeploiement();
deploiment.setNamespace(namespace);
deploiment.setService(service);
deploiment.setGitCommit(historiqueDeploiementReadingDTO.getGit_commit());
deploiment.setTagVersion(historiqueDeploiementReadingDTO.getTag_version());
deploiment.setActionBy(historiqueDeploiementReadingDTO.getAction_by());
historiqueDeploiementRepository.save(deploiment);
log.info("[Historisation] Ajout réalisé pour -> Namespace : " + historiqueDeploiementReadingDTO.getNamespace() + " -> Service : " + historiqueDeploiementReadingDTO.getService() + " -> Tag : " + historiqueDeploiementReadingDTO.getGit_commit());
return deploiment;
}catch(Exception e) {
log.error("[Historisation] Ajout réalisé pour -> Namespace : " + historiqueDeploiementReadingDTO.getNamespace() + " -> Service : " + historiqueDeploiementReadingDTO.getService() + " -> Tag : " + historiqueDeploiementReadingDTO.getGit_commit());
log.warn("[@Transactional] Rollback exécuté");
throw new InternalServerErrorException("Une erreur est survenue lors de l'ajout de l'historique de déploiement");
}
}
在我的主要课程上:
@SpringBootApplication
@EnableTransactionManagement // Added for testing, because without it does not work too.
我希望有人知道为什么不调用回滚。