Re: Cayenne and multi-threading

From: Giulio Cesare Solaroli (giulio.cesar..mail.com)
Date: Fri Dec 17 2004 - 13:18:09 EST

  • Next message: Andrus Adamchik: "Re: Cayenne and multi-threading"

    Hi Andrus,

    On Fri, 17 Dec 2004 10:39:15 -0500 (EST), Andrus Adamchik
    <andru..bjectstyle.org> wrote:
    > Hi there,
    >
    > What type of application do you have in mind?

    We are using Echidna (http://www.javagroup.org/echidna/) to run many
    different tasks inside a single VirtualMachine. The different threads
    can run on their own, but they need to communicate with a coordinator,
    running in the main thread.

    > Anyway, a general rule of thumb is to have a single DataContext to serve each thread.

    This is very reasonable and we should have no problem to comply with
    this constraint.

    > Cayenne stack > underneath DataContext is thread-safe, but DataObjects are not by default.
    > DataContext takes extra precautions to avoid race conditions on commit,
    > i.e. if a user clicks "submit" twice in a web form, but setters are not synchronized.

    That's fine.
     
    > So having multiple threads accessing objects coming from the same
    > DataContext is not a good idea, but if you have to do so, you have to use
    > custom synchronization.

    We are trying to use Cayenne in this project mainly to avoid some
    multi-threading issues bound to the jdbc connection, so I will try to
    stay as far away as possible from trouble. ;-]

    > A better pattern for using DataContext in a multi-threaded environment is
    > to bind one to a thread, and then retrieve it from current thread when
    > needed. We have an example of how this can be done in web application
    > environment, but generally any type of applications can take advantage of
    > that:
    >
    > // thread starts (or a "worker" thread is checked out of the pool)
    > // obtain/create DC, bind it to the thread
    > DataContext context = ...
    > DataContext.bindThreadDataContext(context);
    >
    > ....
    >
    > // Somewhere in the middle of thread processing...
    > // This call retrieves context previously bound to
    > // the current thread from anywhere in the thread call stack
    > DataContext context = DataContext.getThreadDataContext();

    Does the bindThreadDataContext() method do something special, or could
    be enough to store the dataContext in an instance variable of the
    object we use to fork the thread?

    > > I thought this would have been almost a FAQ
    >
    > We need a Wiki... I meant to deploy one for some time, but somehow this
    > fell through the cracks...

    While one is busy developing is not easy to keep up with documentation
    as well!! ;-]

    Thank you very much for your answer.

    Regards,

    Giulio Cesare

    > > Hi all,
    > >
    > > I would like to know which steps needs to be taken to use Cayenne in a
    > > multi-threaded application. I thought this would have been almost a FAQ,
    > > but I haven't been able to find any information on this topic.
    > >
    > > Can anyone give me some pointers to documentation and/or examples on how
    > > to safely use Cayenne in a multi-threaded applications?
    > >
    > > Thanks for your attention.
    > >
    > > Best regards,
    > >
    > > Giulio Cesare Solaroli
    >
    >



    This archive was generated by hypermail 2.0.0 : Fri Dec 17 2004 - 13:18:11 EST