Unless something has changed recently, DataContexts are not thread-safe.

As has been stated before, creating a DataContext is a cheap operation
(populating it from the database is expensive, copying objects from
another data context is cheap).

DataContexts are not tied to connnection pools -- a connection is
temporarily assigned whenever you commit or query the database, and
then released back to the pool.


On Wed, May 2, 2012 at 11:34 AM, Andrew Willerding
<[email protected]> wrote:
> On 05/02/2012 10:18 AM, John Huss wrote:
>>
>> The number of ObjectContexts (or DataContexts) has no relation to the
>> number of database connections.  ObjectContexts are cheap to create and
>> use, so go wild.  Cayenne will do connection pooling out of the box by
>> using the minimum and maximum connection limits that you define in
>> Modeler.
>>
>> John
>>
>>
> That was what I was hoping for.  Continue to let the "magic" take care of
> the nitty-gritty details so I can focus on my app.
>
> But just to confirm my understanding.  I can create one
> DataContext(ObjectContext) and perform as many "simultaneous i.e. from
> multiple threads" object creations, object updates, stored procedure
> executions without concern as long as these creations/updates/stored
> procedure executions are non-related and independent meaning one thread
> that's doing a creation/update/stored procedure execution is not dependent
> on a creation/update/execution going on in another thread.  Something like
> this
>
> public class Main{
>  public  static synchronized DataContext getDataContext() {
>        if (dc == null) {
>            String fileName = "etc/cayenne.xml";
>            org.apache.cayenne.conf.FileConfiguration conf = new
> org.apache.cayenne.conf.FileConfiguration(fileName);
>
>  org.apache.cayenne.conf.Configuration.initializeSharedConfiguration(conf);
>
>            dc = DataContext.createDataContext();
>        }
>        return dc;
>    }
> }
>
> public class myThread(){
>
>    public void run(){
>        DataContext dc = Main.getDataContext();
>
>        // etc on object creation/update/stored procedure executions
>        dc.commitChanges();
>    }
> }
>
>
>
> Andrew

Reply via email to