views:

24

answers:

1

Hi,

I have HttpSessionListener to listen for when sessions are created and destroyed. I have a user domain which has loggedIn boolean column which I update whenever user logs in or logs out which I use for admin management. I also store the session Id in the database.

I also want to update the loggedIn column whenever the session is destroyed. Below is the code written in the sessionDestroyed method.

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

The problem is the user table never gets updated.

Below is the error reported in log file:

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

Can I know how the proper way to update the user table when session is destroyed.

Thank You. Jay Chandran.

+1  A: 

try

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

or

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

or perhaps inject a service that does what you need it to do, as service methods should have transactions by default.

edit -- usually I don't go this far, but Im in a good mood today...something like the following should work. You should really read the grails documentation or buy a book...

User.withTransaction( txStatus -> 
    def user = User.findByUserSessionId(session.getId())
    if(user) {      
       user.setLoggedIn(false)
       user.setUserSessionId("SESSION DESTROYED")
       user.save(flush: true)      
    }
}
hvgotcodes
@jay thats completely unreadable. Please update your original question or start a new one.
hvgotcodes
Sorry about the previous comment. I could not get it formatted properly.
Jay Chandran
@jay, you cannot format code in comments. Either update your question with the new error, or start a new question. Seems like you made progress because the exception changed.
hvgotcodes
Thank You very much for your help. It worked. :) Yeah I am reading docs and book. Started using grails just over 2 months back. Lots to learn.
Jay Chandran
@jay, np glad i could help. Start with the grails documentation itself, there are examples on there for this type of thing....
hvgotcodes
Thanks. Cheers... :)
Jay Chandran