简体   繁体   English

为什么domainObject.discard()在Grails的@Transactional服务中不起作用?

[英]Why does domainObject.discard() not work in @Transactional service in Grails?

I have a method in my service, with @Transactional annotation. 我的服务中有一个方法,带有@Transactional批注。 Inside, I'm doing various stuff with 2 objects, and at the end, I want to .discard() changes on one of them, so it's not saved in this transaction. 在内部,我要对2个对象进行各种处理,最后,我想对其中一个对象进行.discard()更改,因此它不会保存在此事务中。 What I do is send information about object2 to my ActiveMQ queue, where it's picked up by a listener, there's some work done there and in the end it saved in another transaction. 我要做的是将有关object2的信息发送到我的ActiveMQ队列中,由侦听器在其中进行一些工作,最后将其保存在另一个事务中。 Object1 is supposed to save as usual. 应该将Object1照常保存。

This however, does not work. 但是,这不起作用。 What happens is, that listener on my queue is able to .get() the object, perform some changes on it and .save() it, before the first transaction is over. 发生的是,在第一个事务结束之前,队列中的侦听器能够.get()对象,对其进行一些更改,并对其进行.save() Thanks to that, I get org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) . 由于这一点,我得到了org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

I perform .get() of the objects inside the service. 我在服务内部执行.get()对象。 Transaction is saved when exiting service and coming back to the controller. 退出服务并返回到控制器时,将保存事务。

What do I have to do to prevent Grails from saving object2 in the main transaction? 如何防止Grails在主事务中保存object2?

You could try a .refresh() on object2 before the end of your service method. 您可以在服务方法结束之前在object2上尝试.refresh() This will reload and overwrite any data in object2 with the updated data in your database, so the optimistic locking version variable matches (which I think is what is causing your Exception ). 这将使用数据库中的更新数据重新加载并覆盖object2中的任何数据,因此,乐观锁定version变量会匹配(我认为这是导致您Exception )。 And Hibernate won't need to flush the object as it now matches the database. 而且Hibernate不需要刷新对象,因为它现在与数据库匹配。

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

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