title: Could not synchronize database state with session Error in Grails Hibernate
body: <p>I have registered a <strong>MySecurityEventListener</strong> in my Grails app to set a login count after a user has been logged in.</p>
  
  <p>MySecurityEventListener class: </p>
  
  <pre><code>class MySecurityEventListener implements ApplicationListener&lt;InteractiveAuthenticationSuccessEvent&gt;, LogoutHandler {
  
  
  /**
   * Handler for after login.
   */
  @Override
  public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
  
      User.withNewSession {
          User user = User.get(event.authentication.principal.id)
          user.lastLoginDate = new Date()     // set the last login date
          user.loginCount += 1                // increase the login count
          user.isLoggedIn = true
          user.save(flush: true)
      }
  }
  
  
  /**
   * Handler for after logout.
   */
  @Override
  public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
  
      User.withNewSession {
          User user = User.get(authentication.principal.id)
          user.isLoggedIn = false
          user.save(flush: true)
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Sometimes when a user loggs in I get the following error: </p>
  
  <pre><code>| Error 2013-07-03 13:40:56,306 [http-nio-8080-exec-1] 
  ERROR    events.PatchedDefaultFlushEventListener  - 
   Could not synchronize database state with session Message: 
  Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [demo.User#ffd93c5639b54405bf]
  Line | Method
  -&gt;&gt;   38 | doCall             in  demo.MySecurityEventListener$_onApplicationEvent_closure1
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |     33 | onApplicationEvent in demo.MySecurityEventListener
  |   1145 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
  |    615 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
  ^    722 | run . . . . . . .  in java.lang.Thread
  2013-07-03 13:40:56,551 [http-nio-8080-exec-1] INFO  cpr.SessionSupport  - Session created
  2013-07-03 13:40:56,554 [http-nio-8080-exec-2] INFO  cpr.SessionSupport  - Session created
  
  | Error 2013-07-03 13:40:56,554 [http-nio-8080-exec-1] ERROR [/demo].[gsp]  -  Servlet.service() for servlet [gsp] in context with path [/demo] threw exception
  Message: Object of class [demo.User] with identifier [ffd93c5639b54405bf]: optimistic   locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated   or deleted by another transaction (or unsaved-value mapping was incorrect):    [demo.User#ffd93c5639b54405bf]
  Line | Method
  -&gt;&gt;   38 | doCall             in    demo.MySecurityEventListener$_onApplicationEvent_closure1
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |     33 | onApplicationEvent in demo.MySecurityEventListener
  |   1145 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
  |    615 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
  ^    722 | run . . . . . . .  in java.lang.Thread
  Caused by StaleObjectStateException: Row was updated or deleted by another transaction     (or unsaved-value mapping was incorrect): [demo.User#ffd93c5639b54405bf]
  -&gt;&gt;   38 | doCall             in  demo.MySecurityEventListener$_onApplicationEvent_closure1 
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |     33 | onApplicationEvent in demo.MySecurityEventListener
  |   1145 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
  |    615 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
  ^    722 | run . . . . . . .  in java.lang.Thread
  </code></pre>
  
  <p>How do I have to fix this error?</p>