简体   繁体   中英

Could not synchronize database state with session Error in Grails Hibernate

I have registered a MySecurityEventListener in my Grails app to set a login count after a user has been logged in.

MySecurityEventListener class:

class MySecurityEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent>, LogoutHandler {


/**
 * Handler for after login.
 */
@Override
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {

    User.withNewSession {
        User user = User.get(event.authentication.principal.id)
        user.lastLoginDate = new Date()     // set the last login date
        user.loginCount += 1                // increase the login count
        user.isLoggedIn = true
        user.save(flush: true)
    }
}


/**
 * Handler for after logout.
 */
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {

    User.withNewSession {
        User user = User.get(authentication.principal.id)
        user.isLoggedIn = false
        user.save(flush: true)
    }

}

}

Sometimes when a user loggs in I get the following error:

| Error 2013-07-03 13:40:56,306 [http-nio-8080-exec-1] 
ERROR    events.PatchedDefaultFlushEventListener  - 
 Could not synchronize database state with session Message: 
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [demo.User#ffd93c5639b54405bf]
Line | Method
->>   38 | doCall             in  demo.MySecurityEventListener$_onApplicationEvent_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     33 | onApplicationEvent in demo.MySecurityEventListener
|   1145 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . .  in java.lang.Thread
2013-07-03 13:40:56,551 [http-nio-8080-exec-1] INFO  cpr.SessionSupport  - Session created
2013-07-03 13:40:56,554 [http-nio-8080-exec-2] INFO  cpr.SessionSupport  - Session created

| Error 2013-07-03 13:40:56,554 [http-nio-8080-exec-1] ERROR [/demo].[gsp]  -  Servlet.service() for servlet [gsp] in context with path [/demo] threw exception
Message: Object of class [demo.User] with identifier [ffd93c5639b54405bf]: optimistic   locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated   or deleted by another transaction (or unsaved-value mapping was incorrect):    [demo.User#ffd93c5639b54405bf]
Line | Method
->>   38 | doCall             in    demo.MySecurityEventListener$_onApplicationEvent_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     33 | onApplicationEvent in demo.MySecurityEventListener
|   1145 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . .  in java.lang.Thread
Caused by StaleObjectStateException: Row was updated or deleted by another transaction     (or unsaved-value mapping was incorrect): [demo.User#ffd93c5639b54405bf]
->>   38 | doCall             in  demo.MySecurityEventListener$_onApplicationEvent_closure1 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     33 | onApplicationEvent in demo.MySecurityEventListener
|   1145 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . .  in java.lang.Thread

How do I have to fix this error?

withTransaction provides access to the underlying transaction. If needed a control over transaction rollback you can use it.

withSession uses the default session provided by SessionFactory. If needed a control over sesion then use it.

Looking at your example, I would vouch for withTransaction (the simplest one).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM