Cardboard developer, I reread my post and I see the answer, the Thread in
getThreadObjectContext makes all the difference. However, I need to gain access
to the ObjectContext outside of servlet code. Should I use
BaseContext.bindThreadObjectContext(ServletUtil.getSessionContext(session))
within each servlet?
-----Original Message-----
From: Kuhns, John [mailto:[email protected]]
Sent: Tuesday, June 15, 2010 10:16 AM
To: [email protected]
Subject: 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