1

我在我的 Grails 应用程序中注册了一个MySecurityEventListener以在用户登录后设置登录计数。

MySecurityEventListener 类:

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)
    }

}

}

有时,当用户登录时,我会收到以下错误:

| 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

我该如何解决这个错误?

4

1 回答 1

1

withTransaction提供对底层事务的访问。如果需要控制事务回滚,您可以使用它。

withSession使用 SessionFactory 提供的默认会话。如果需要控制会话,请使用它。

看看你的例子,我会保证withTransaction(最简单的一个)。

于 2013-07-03T13:35:35.320 回答