Re: Current thread has no bound DataContext (zk integration)

From: Steve Wells (websystem..mail.com)
Date: Sun Sep 14 2008 - 22:07:07 EDT

  • Next message: Andrus Adamchik: "Re: Cayenne, XML objects and JDBC 4"

    Just for the record I found an answer (untested) to this over at the ZK
    list:

    From: henrichen

    1. ZK by default spawn a new event thread for each "event" handling unless
    you tell it not to. That is, unless you specify following in WEB-INF/zk.xml

      <system-config>
      <disable-event-thread/>
      </system-config>

    2. I "guess" Cayanne "hold" something in its ThreadLocal variables for each
    request in servlet thread.

    3. As said on 1, when a ZK event is going to be handled, a new event thread
    is spawn and run the event handling codes in the spawned event thread.

    4. Therefore the ThreadLocal variables prepared by Cayanne does not exist in
    the event thread (though they exist in servlet thread) and cause issues.

    5. ZK event thread mechanism is the base of the ZK modal window such as
    Messagebox.
    That is, if you disable the event thread mechanism as said in 1., you cannot
    use ZK modal window, either. If you don't need the ZK modal window, disable
    the ZK event thread mechanism shall solve your issue per your experiment.

    6. OK. If you do need ZK's modal window support, here is an alternative. ZK
    provides a way that will "copy" thread local variables from servlet thread
    to
    event thread and vice versa automatically for you. However, you have to tell
    it what ThreadLocal variables to copy.

    e.g. To make ZK works well with Spring Security, you will have to specify in
    WEB-INF/zk.xml following codes.

    <listener>
      <description>ThreadLocal Synchronization Listener</description>
      <listener-class>org.zkoss.zkplus.util.ThreadLocalListener</listener-class>
    </listener>

    <preference>
      <name>ThreadLocal</name>
      <value>
      org.springframework.security.context.ThreadLocalSecurityContextHolderSt
    rategy=contextHolder
      </value>
    </preference>

    For Cayanne, you have to find out the associated ThreadLocal variables and
    specify
    it in the <preference> in the form of

      <value>
      class=variable
      </value>

    Please check the Javadoc of ThreadLocalListener for details

    2008/8/26 Mike Kienenberger <mkienen..mail.com>

    > Mambo, you might also take a look at Steve's other thread on ZK and
    > thread-safety issues.
    >
    > http://osdir.com/ml/java.cayenne.user/2007-05/msg00067.html
    >
    > On 8/24/08, Steve Wells <websystem..mail.com> wrote:
    > > The way I seem (seem as I am yet to *fully* test this) to have solved
    > that
    > > is to use Spring to create the ObjectContext and then you inject that in
    > to
    > > whatever else you want. This will not be Thread bound but Singleton, I
    > > guess in Spring you could write (or copy) a Thread bound scope if
    > required.
    > > eg:
    > > <bean id="myObjectContext"
    > class="org.apache.cayenne.access.DataContext"
    > > factory-method="createDataContext" scope="singleton">
    > > <constructor-arg value="MyDomain"/>
    > > </bean>
    > > <!-- A data layer controller/convenience function layer -->
    > > <bean id="dataServices" class="com.mypackage.dataservices.DataServices">
    > > <property name="objectContext">
    > > <ref local="myObjectContext"/>
    > > </property>
    > > </bean>
    > > public class DataServices {
    > > private ObjectContext objectContext;
    > >
    > > public ObjectContext getObjectContext() {
    > > return objectContext;
    > > }
    > >
    > > public void setObjectContext(ObjectContext objectContext) {
    > > this.objectContext = objectContext;
    > > }
    > >
    > >
    > > HTH
    > > Steve
    > > 2008/8/23 mambo <ikonicpattern..mail.com>
    > >
    > >
    > > > I've been looking through the mailing list and I can't find a definite
    > > > answer. I'm using ZK, I have added:
    > > > <filter>
    > > > <filter-name>CayenneFilter</filter-name>
    > > >
    > > >
    > <filter-class>org.apache.cayenne.conf.WebApplicationContextFilter</filter-class>
    > > > </filter>
    > > > <filter-mapping>
    > > > <filter-name>CayenneFilter</filter-name>
    > > > <url-pattern>/*</url-pattern>
    > > > </filter-mapping>
    > > >
    > > > to my web.xml. When I try "DataContext context =
    > > > DataContext.getThreadDataContext();" I get an exception that says
    > "Current
    > > > thread has no bound DataContext". I'm figuring this is because ZK is
    > multi
    > > > threaded. I'm pretty new to Java so I'm not really comfortable trying
    > to
    > > > hack the cayanne source to get things to run. Is there a solution for
    > this?
    > > > If i created a new Datacontext with each Database request, how badly
    > would
    > > > that affect performance of my web app?
    > > >
    > >
    >



    This archive was generated by hypermail 2.0.0 : Sun Sep 14 2008 - 22:07:48 EDT