Spring boot 在SpringBoot中不回滚
我想创建一个事务性方法,以便在发生任何错误时删除数据库中的插入 我的测试: 我称之为checkNamespaceAndService.getNamespace是成功的,所以可以,但由于验证原因,deployment.setGitCommit失败。因此会抛出一个异常 但在数据库中,保留第一种方法的结果Spring boot 在SpringBoot中不回滚,spring-boot,transactions,Spring Boot,Transactions,我想创建一个事务性方法,以便在发生任何错误时删除数据库中的插入 我的测试: 我称之为checkNamespaceAndService.getNamespace是成功的,所以可以,但由于验证原因,deployment.setGitCommit失败。因此会抛出一个异常 但在数据库中,保留第一种方法的结果 @Transactional(rollbackFor=Exception.class) public HistoriqueDeploiement saveHistoriqueDeploiem
@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.
我希望有人知道为什么不调用回滚。您是从同一个类调用save方法还是从不同的类调用save方法;在我从控制器调用的事务性方法中:SaveHistoryQuedeLoviementMethod