spring - StaleObjectStateException: Row was updated or deleted by another transaction? -
i following:
def currentuser = springsecurityservice.currentuser currentuser.name = "test" currentuser.save(flush: true) // other code currentuser.gender = "male" currentuser.save(flush: true) // exception occurs
this exception get:
error events.patcheddefaultflusheventlistener - not synchronize database state session org.hibernate.staleobjectstateexception: row updated or deleted transaction (or unsaved-value mapping incorrect)
how can prevent error? best solution that?
i found different approaches:
which 1 should use?
you should use merge - update object match current state in database. if use discard reset object database has, discarding changes. else in hibernate session need manage yourself.
more importantly code should written in service there database transaction, , should use
save(flush:true)
once @ end.
def currentuser = springsecurityservice.currentuser currentuser.name = "test" // currentuser.save(flush: true) // removing line because if rollback occurs, changes before persisted. // other code currentuser.gender = "male" currentuser.merge() // merge persistent object current state currentuser.save(flush: true)
Comments
Post a Comment