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:

  1. here can use discard()
  2. here can use merge()

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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -