title: Last login date in grails with spring security
body: <p>I am running into this error when I try to set a <code>lastLogin</code> date with an event listener in <code>Config.groovy</code>:</p>
  
  <blockquote>
    <p>2011-05-12 00:30:16,501 ["ajp-bio-8009"-exec-6] ERROR
    events.PatchedDefaultFlushEventListener  - Could not synchronize
    database state with session org.hibernate.StaleObjectStateException:
    Row was updated or deleted by another transaction (or unsaved-value
    mapping was incorrect): [spl.User#110]</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx -&gt;
      User.withTransaction {
          def user = appCtx.springSecurityService.currentUser
          user.lastLogin = new Date()
          user.save(flush: true)
      }
  }
  </code></pre>
  
  <p>I can't seem to replicate this in my test environment but the one time I tried it live in production I saw this error and decided to revert back. Will changing save to merge solve the problem?</p>
  
  <p><strong>EDIT: I was able to replicate the error in my test environment by signing on as the same user simultaneously. To debug further, I added some print statements inside of the event handler and went live with it. It turns out that when I sign or anyone signs on to my site, the event handler gets called MULTIPLE times, hence stale object exception. Why is the onInteractiveAuthenticationSuccessEvent getting called more than once on a login?</strong></p>
  
  <p>Thanks, 
  Ricardo</p>