开发者

Last login date in grails with spring security

I am running into this error when I try to set a lastLogin date with an event listener in Config.groovy:

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]

My code:

grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
    User.withTransaction {
        def user = appCtx.springSecurityService.currentUser
        user.lastLogin = new Date()
        user.save(flush: true)
    }
}

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?

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 si开发者_JAVA百科te, the event handler gets called MULTIPLE times, hence stale object exception. Why is the onInteractiveAuthenticationSuccessEvent getting called more than once on a login?

Thanks, Ricardo


You'll want to get a fresh copy of the user in the session first:

grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
    User.withTransaction {
        def user = User.get(appCtx.springSecurityService.currentUser.id)
        user.lastLogin = new Date()
        user.save(flush: true)
    }
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜