2010-10-16 61 views
2

我有HttpSessionListener来侦听会话何时被创建和销毁。我有一个已登录的用户域,在用户登录或注销时用于管理管理的情况下,我会更新该布尔列。我还将会话ID存储在数据库中。Grails 1.3.5:会话被破坏时更新用户表

我也想在会话销毁时更新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) 

我能知道什么时候会被破坏的正确方法如何更新用户表。

谢谢。 Jay Chandran。

回答

2

尝试

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

User.withSession{ session - > .... } 

或者是注入一个服务,做什么,你需要做的,作为服务方法应该有预设的交易。

编辑 - 通常我不走这么远,但我今天心情很好......像下面这样的东西应该可以工作。你应该真的阅读Grails文档或购买一本书...

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

@jay thats completely unreadable。请更新您的原始问题或开始一个新的问题。 – hvgotcodes 2010-10-16 19:39:53

+0

对此前的评论感到抱歉。我无法正确格式化。 – 2010-10-16 19:46:16

+0

@jay,你不能在注释中格式化代码。请用新错误更新您的问题,或者开始一个新问题。似乎你取得了进展,因为例外发生了变化。 – hvgotcodes 2010-10-16 19:47:49