使用Grails GORM和MySQL的乐观锁定异常

时间:2022-01-30 00:54:34

I am using Grails 2.4.4 and MySQL database. We are trying to clone a group of tables in our database having parent-child relationship. For example we have a table author with its child table book and book having foreign key fk_book pointing to author.

我正在使用Grails 2.4.4和MySQL数据库。我们正在尝试克隆具有父子关系的数据库中的一组表。例如,我们有一个表作者及其子表书和书,其中外键fk_book指向作者。

We retrieve the hierarchy through Author.get(id), the cloning author, its associated book and save using clonedAuthor.save(flush:true). We have hierarchy of several tables like this, some having OneToOne and some having OneToMany relationships. In database, the foreign key constraints are given as ON DELETE NO ACTION.

我们通过Author.get(id),克隆作者及其相关书籍检索层次结构,并使用clonedAuthor.save(flush:true)保存。我们有这样的几个表的层次结构,一些具有OneToOne,一些具有OneToMany关系。在数据库中,外键约束为ON DELETE NO ACTION。

The problem is when invoked. It is throwing Optimistic Locking Failure Exception

问题是在被调用时。它正在抛出乐观的锁定失败例外

Row was updated or deleted by another transaction.

I am not getting which transaction is updating the row since we have only one transaction (only one hit).

我没有得到哪个事务正在更新行,因为我们只有一个事务(只有一个命中)。

2 个解决方案

#1


I was getting same problem when modifying db data using mysql console while some of the data is already fetched to the hibernate session.

我在使用mysql控制台修改数据库时遇到了同样的问题,而一些数据已经被提取到hibernate会话。


why is this happening?

为什么会这样?

Just simple, the data in db and in hibernate session are out of sync.

很简单,db和hibernate会话中的数据不同步。

Also if you are putting the domain object ( or any related domain object) in session try to re-attach the object(obj.attach()) before calling save,delete..

此外,如果您在会话中放置域对象(或任何相关的域对象)尝试在调用save之前重新附加对象(obj.attach()),请删除..

#2


You have 3 solutions: merge() or attach() before save()... or mapping the entity with version false.

您有3个解决方案:save()之前的merge()或attach()...或者使用版​​本false映射实体。

#1


I was getting same problem when modifying db data using mysql console while some of the data is already fetched to the hibernate session.

我在使用mysql控制台修改数据库时遇到了同样的问题,而一些数据已经被提取到hibernate会话。


why is this happening?

为什么会这样?

Just simple, the data in db and in hibernate session are out of sync.

很简单,db和hibernate会话中的数据不同步。

Also if you are putting the domain object ( or any related domain object) in session try to re-attach the object(obj.attach()) before calling save,delete..

此外,如果您在会话中放置域对象(或任何相关的域对象)尝试在调用save之前重新附加对象(obj.attach()),请删除..

#2


You have 3 solutions: merge() or attach() before save()... or mapping the entity with version false.

您有3个解决方案:save()之前的merge()或attach()...或者使用版​​本false映射实体。