简体   繁体   English

Spring 数据 jpa 多个实体未回滚 - PostgreSQL

[英]Spring data jpa with multiple entities not rolling back - PostgreSQL

I am using spring-data-jpa in one of my projects.我在我的一个项目中使用 spring-data-jpa。 In service layer, I have annotated a private method with @Transactional and also enabled @EnableTransactionManagement in application.在服务层,我用@Transactional 注释了一个私有方法,并在应用程序中启用了@EnableTransactionManagement。 When one of the save method of entities throws an exception, the rest of the entities which were saved before are not rolling back.当实体的保存方法之一抛出异常时,之前保存的实体的 rest 不会回滚。 BTW I am using PostgreSQL.顺便说一句,我正在使用 PostgreSQL。

Please let me know if I am missing anything here.如果我在这里遗漏任何东西,请告诉我。

Spring transaction will only work with public method. Spring 事务仅适用于公共方法。 As it need to inject code using proxy classes for transactions.因为它需要使用代理类为事务注入代码。 So making your method public will resolve your issue.因此,公开您的方法将解决您的问题。 Have a look on documentation of proxy mechanism of spring .查看spring 的代理机制的文档

Spring by default will rollback only for Runtime Exceptions ( https://docs.spring.io/spring/docs/2.5.x/reference/transaction.html#transaction-declarative ). Spring 默认情况下只会回滚运行时异常( https://docs.spring.io/spring/docs/2.5.x/reference.transaction#transaction

If you want to rollback for any exception, you could try adding:如果要回滚任何异常,可以尝试添加:

@Transactional(rollbackFor = Exception.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM