我正在使用Spring Security进行身份验证。我正在使用登录事件监听器来记录用户的上次登录时间
在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)
}
}
}
我现在得到的错误是
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是错误的原因吗?我感谢任何帮助!谢谢!