Re: Cayenne and multi-threading

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Dec 17 2004 - 10:39:15 EST

  • Next message: Mike Kienenberger: "Re: Cayenne for SIM-like game"

    Hi there,

    What type of application do you have in mind? Anyway, a general rule of
    thumb is to have a single DataContext to serve each thread. 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.

    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.

    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();

    > 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...

    Andrus

    > 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 - 10:39:16 EST