简体   繁体   English

休眠事务承诺

[英]Hibernate Transactional Commits

I am working in a spring boot app, and I have some methods that go through a list and perform multiple commits to the database through hibernate. 我在Spring Boot应用程序中工作,我有一些方法可以遍历列表,并通过休眠对数据库执行多次提交。

For example, I receive an object from the front end that the user has modified, and that object has many samples associated to it, and each sample has many tests associated to it. 例如,我从前端收到一个用户已修改的对象,并且该对象具有与之关联的许多示例,而每个示例均具有与之相关的许多测试。

In this scenario, i need to generate a copy of the object, its samples, and the tests and save them. 在这种情况下,我需要生成对象,其样本和测试的副本并保存它们。

The issue im having is if somewhere in the middle an exception happens, hibernate only rolls back the most recent commit action. 问题是如果中间发生异常,休眠只会回滚最近的提交动作。 But i need to roll back everything from the start. 但是我需要从一开始就回滚所有内容。

I am still new to using hibernate and spring, so my question is if you have a method that is performing multiple repository.save() calls (because you are saving across multiple tables) and something goes wrong, how can you roll back all the save() actions you have performed during this transaction? 我对使用休眠和弹簧还很陌生,所以我的问题是,如果您有一个方法正在执行多个repository.save()调用(因为您要在多个表之间进行保存)而出了点问题,那么如何回滚所有您在此交易期间执行的save()动作?

Is there an annotation i can put on my function that does this for me? 我可以在函数上添加注释吗?

Using the annotation bellow on method from service, should help you: 在服务的方法上使用下面的注释注释应该可以帮助您:

@Transactional(rollbackFor = Exception.class)

You must throw an exception somewhere in service method and the Spring will roll back the transaction if a thrown error is occurred. 您必须在服务方法的某处引发异常,如果发生引发的错误,Spring将回滚事务。

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

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