hi, maybe this "ancient" article by Brian Goetz may help you: http://www.ibm.com/developerworks/library/j-jtp09238/index.html :)
cheers, Kris On Sat, Jan 18, 2014 at 8:39 AM, Ilya Obshadko <ilya.obsha...@gmail.com>wrote: > Hello, > > I have implemented persistent session management in my application (using > cookies and session IDs in the database). Everything works fine, but > sometimes I see Hibernate errors "Batch update returned unexpected row > count from update" when committing database changes after setting up a new > session. > > It turns out that it happens when user starts a request and then quickly > initiates reload of the same page. So I suppose this is a threading issue > (one thread has already started creating session ID record, then user > interrupts the connection and starts another request while the previous one > hasn't been completed). I have implemented simple synchronization, but not > yet sure if it's done correctly. > > So the question is: how do I synchronize method that creates the session? > > Currently it's implemented inside my authentication service: > > public void setupSessionForUser ( User user ) { > > synchronized ( this ) { > > user.clearSessions (); > > > > SessionToken newSessionToken = SessionToken.create (); > > user.addSessionToken ( newSessionToken ); > > hibernateSession.save ( newSessionToken ); > > asm.set ( User.class, (User) hibernateSession.merge ( user ) ); > > cookies.writeCookieValue ( "authcookiename", > newSessionToken.toString () ); > > logger.info ( "set up new session {} for {}", > newSessionToken.toString (), user.getEmail () ); > > } > > } > > Is that correct to synchronize on *this*? Or should I synchronize on HTTP > Session instance instead? > > Thanks in advance. > > -- > Ilya Obshadko >