Grails 登录事件侦听器出错?

Grails 登录事件侦听器出错?,grails,spring-security,Grails,Spring Security,我正在使用Spring安全性进行身份验证。我正在使用登录事件侦听器记录用户上次登录的时间 在conf/spring/resources.groovy中 loginEventListener(LoginEventListener) 监听器被定义为 class LoginEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> { //deal with su

我正在使用Spring安全性进行身份验证。我正在使用登录事件侦听器记录用户上次登录的时间

在conf/spring/resources.groovy中

loginEventListener(LoginEventListener)
监听器被定义为

class LoginEventListener implements
    ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    //deal with successful login
    void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        User.withTransaction {

            def user = User.findById(event.authentication.principal.id)
            if(!user.isAttached())
                user.attach()
            user.lastLoginTime = new Date() // update login time
            user.save(flush: true, failOnError: true)


        }


    }


}
错误如下

ERROR 2017-05-31 16:04:15,715 [ajp-bio-8109-exec-9659] 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): [User#7]
    at LoginEventListener$_onApplicationEvent_closure1.doCall(LoginEventListener.groovy:18)
    at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:686)
    at LoginEventListener.onApplicationEvent(LoginEventListener.groovy:12)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我正在试图了解此错误的原因,以及如何避免它?flush:true是错误的原因吗?谢谢你的帮助!谢谢

尝试
User.withNewTransaction{User=User.get(event.authentication.principal.id)
findById:)谢谢,但我也想知道原因。您知道此错误的原因吗?
行已被另一个事务更新或删除
您可能触发了另一个事件事务,该事务正在别处查找记录,但仍挂起对象,您尝试删除它时失败。执行新事务Action意味着你得到了一个新的实例。当你有withTransaction的时候,通常你不需要刷新:也是真的-但使用not Use all会根据情况产生不同的效果-所以我的答案不是,答案是关于反复调整某些东西和测试行为-也要重新启动应用程序,因为我注意到有一些删除调用启动一个应用程序时,有时会发出以前有效的调用,但在运行一个干净的实例时不起作用。因此,请始终在新运行的appok上测试您的上一个工作代码。谢谢,我会看看这是否解决了问题
ERROR 2017-05-31 16:04:15,715 [ajp-bio-8109-exec-9659] 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): [User#7]
    at LoginEventListener$_onApplicationEvent_closure1.doCall(LoginEventListener.groovy:18)
    at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:686)
    at LoginEventListener.onApplicationEvent(LoginEventListener.groovy:12)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)