Grails1.3.5:当会话被破坏时更新用户表

Grails1.3.5:当会话被破坏时更新用户表,grails,grails-domain-class,Grails,Grails Domain Class,我有HttpSessionListener来监听会话的创建和销毁。我有一个用户域,其中有loggedIn布尔列,每当用户登录或注销时,我都会更新该列,我使用该列进行管理。我还将会话Id存储在数据库中 我还想在会话被破坏时更新loggedIn列。下面是使用sessionDestroyed方法编写的代码 def user = User.findByUserSessionId(session.getId()) if(user) { user.setLoggedIn(false)

我有HttpSessionListener来监听会话的创建和销毁。我有一个用户域,其中有loggedIn布尔列,每当用户登录或注销时,我都会更新该列,我使用该列进行管理。我还将会话Id存储在数据库中

我还想在会话被破坏时更新loggedIn列。下面是使用sessionDestroyed方法编写的代码

def user = User.findByUserSessionId(session.getId())
if(user) {      
    user.setLoggedIn(false)
    user.setUserSessionId("SESSION DESTROYED")
    user.save(flush: true)      
 }
问题是用户表永远不会更新

以下是日志文件中报告的错误:

            [2010-10-16 11:45:07.781] ERROR core.ContainerBase.[Tomcat].[localhost].[/SAMPLE] Session event listener threw exception
        org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
            at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
            at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
            at org.codehaus.groovy.grails.orm.hibernate.validation.HibernateDomainClassValidator.validate(HibernateDomainClassValidator.java:66)
            at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractSavePersistentMethod.doInvokeInternal(AbstractSavePersistentMethod.java:129)
            at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractDynamicPersistentMethod.invoke(AbstractDynamicPersistentMethod.java:59)
            at sun.reflect.GeneratedMethodAccessor500.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:188)
            at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
            at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:132)
            at org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport$_addBasicPersistenceMethods_closure71.doCall(HibernatePluginSupport.groovy:812)
我可以知道在会话被破坏时如何正确地更新用户表吗

多谢各位。 杰伊·钱德兰。

试试看

User.withTransaction{ txStatus ->    .... }

或者注入一个服务来完成您需要它做的事情,因为默认情况下服务方法应该有事务

编辑——通常我不会走这么远,但我今天心情很好……像下面这样的事情应该行得通。你真的应该阅读grails文档或者买本书

User.withTransaction( txStatus -> 
    def user = User.findByUserSessionId(session.getId())
    if(user) {      
       user.setLoggedIn(false)
       user.setUserSessionId("SESSION DESTROYED")
       user.save(flush: true)      
    }
}

@杰伊:那完全不可读。请更新您的原始问题或开始一个新的问题。对以前的评论感到抱歉。我无法正确格式化。@jay,您不能在注释中格式化代码。用新错误更新您的问题,或开始新问题。由于异常发生了变化,您似乎取得了进展。非常感谢您的帮助。成功了。:)是的,我在读文件和书。两个多月前开始使用grails。有很多东西要学。@jay,很高兴我能帮上忙。从grails文档本身开始,这里有关于这类事情的示例。。。。
User.withTransaction( txStatus -> 
    def user = User.findByUserSessionId(session.getId())
    if(user) {      
       user.setLoggedIn(false)
       user.setUserSessionId("SESSION DESTROYED")
       user.save(flush: true)      
    }
}