开发者

In grails, why do I get a StaleObjectStateException directly after refresh?

开发者 https://www.devze.com 2023-01-30 00:53 出处:网络
I got code like this: def myObject = MyDomainClass.get(myId) myObject.refresh() myOb开发者_如何学Cject.myProperty = myValue

I got code like this:

def myObject = MyDomainClass.get(myId)
myObject.refresh()
myOb开发者_如何学Cject.myProperty = myValue
myObject.save(flush:true, failOnError:true)

Despite of the get and the refresh, I sometimes get an "org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)" when the save is executed.

It happens when I start to execute this method concurrently in multiple sessions. But then transaction 1 is definitely finished, this code is executed again for transaction 2 and it still fails! (I'm using a transaction service to re-execute transactions when they fail due to optimistic locking, see here).

How can that be although I get a "fresh" version from the DB?


This forum thread hints that you might need another Hibernate Session. What if you try a new session for a new transaction, like

Book.withNewSession{}


I at least found a workaround - rolling back an empty transaction:

myDomain.withTransaction { status -> 
  status.setRollbackOnly()
}


you should use

MyDomainClass.lock(myId)

instead of

MyDomainClass.get(myId)
0

精彩评论

暂无评论...
验证码 换一张
取 消