BaseContext in servlet

From: Kuhns, John (jkuhn..imNTLS.com)
Date: Tue Jun 15 2010 - 14:16:22 UTC

  • Next message: Michael Gentry: "Re: BaseContext in servlet"

    I am attempting to set a reference to a pojo containing some user info for use during updates/inserts. On reading the documentation early on, 3.0RC1 or so, I believed I could use setUserProperty and the context would be reused within a given session. However it appears that BaseContext.getThreadObjectContext() does not return the same context ever.

    In my web.xml I have:
            <filter>
                    <filter-name>CayenneFilter</filter-name>
                    <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
            </filter>
            <filter-mapping>
                    <filter-name>CayenneFilter</filter-name>
                    <servlet-name>/*</servlet-name>
            </filter-mapping>

    I then implemented HttpSessionAttributeListener in my class and used the following code within it:

            public void attributeAdded(HttpSessionBindingEvent arg0) {
                    ObjectContext ctx = null;
                    try{
                            ctx = BaseContext.getThreadObjectContext();
                    }catch(Exception e){}
                    
                    if(ctx != null){
                            BaseContext.bindThreadObjectContext(ctx);
                            ctx.setUserProperty("myData", this);
                    }
            }

    The code gets hit and the property is set, but every subsequent call to BaseContext.getThreadObjectContext().getUserProperty("myData") returns null. If I explicitly set the user property with every request, all works fine. Am I missing something?

    John Kuhns



    This archive was generated by hypermail 2.0.0 : Tue Jun 15 2010 - 14:17:00 UTC