我有一个简单的Spring Security登录事件侦听器注册为
class LoginEventListener implements
ApplicationListener<InteractiveAuthenticationSuccessEvent> {
//deal with successful login
void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
User.withTransaction {
def user = User.get(event.authentication.principal.id)
user.lastLoginTime = new Date() // update login time
user.save()
}
}
}
在日志中,每进行几次登录尝试,我遇到的错误就是
ERROR 2017-06-13 16:24:04,090 [ajp-bio-8109-exec-4561] 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#2876]
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:686)
at com.runnercard.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)
我想知道如何避免此错误?感谢您的帮助!谢谢!
最佳答案
尝试使用锁
def user = User.get(event.authentication.principal.id ,[lock: true])
关于grails - 登录事件监听器中的另一个事务错误更新或删除了行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44536159/