Hi again Andrew!

On Wed, May 2, 2012 at 2:54 PM, Andrew Willerding
<[email protected]> wrote:
> On 05/02/2012 10:35 AM, Michael Gentry wrote:
>>
>> Creating a new DataContext may or may not be the right approach for your
>> application, but to say it "is not a recommended practice" would be
>> incorrect. If your requests are naturally isolated from other requests,
>> there is no harm in creating a new DataContext/request.
>
> I got that quote from the Cayenne Guide
>
> http://cayenne.apache.org/doc30/obtaining-datacontext.html


We should change that wording in the documentation.  Thanks for pointing it out.


> so that was got me thinking about the whole thread topic and what might be
> the recommended practice for a multi-threaded stand-alone app.
>
>
>> DataContexts (parent/child/whatever) are not tied at all to the connection
>> pool limits. Creating a DataContext is cheap, too. Create all that you need.
>
>
> I am beginning to understand this now but in my "extreme" case of 200
> simultaneous requests to a DataContext can I simply create 200 new
> DataContexts using code like
>
>
>           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);
>           DataContext dc = DataContext.createDataContext();
>
>
> without causing multiple open/close connections to the underlying database?


200 threads simultaneously using a DataContext is going to be bad.
You want 200 DataContexts.  Also, you don't need to initialize the
shared configuration every time you create a DataContext.  Just once
should be fine.

When you create a DataContext, it doesn't open/close a connection to
the database.  Doing dataContext.performQuery() or
dataContext.commitChanges() (and similar fetch/commit calls) will get
a database connection from the connection pool, use it, then return
it.


> And where does the underlying connection pool set for lets say 20 maximum
> connections come in to play?  In the code example above, will a
> createDataContext() fail at the 21st request?


If you have a maximum of 20 connections and you have 20 simultaneous
fetches/commits going on, the 21st will have to wait or fail.  If you
use the DBCPDataSourceFactory method (instead of
DriverDataSourceFactory), you get more control in configuring such
things.  See http://cayenne.apache.org/doc/dbcpdatasourcefactory.html
and cayenne.dbcp.maxWait, for example.

Keep in mind a DataContext only grabs a database connection when it
actually needs to talk to the database.  If your request takes 1000ms
to process, but only needs 100ms of database connection usage time,
that's 900ms of time where the DataContext is not using a connection
and another thread/DataContext can use the connection.

Furthermore, of that 100ms of database connection usage time, let's
imagine that you first do a fetch which takes 20ms and then you do an
update which takes 80ms.  The DataContext is going to checkout (and
checkin) the database connection from (and to) the pool twice: once
for the select, once for the update.  You may get the same connection
the second time or perhaps a different connection.  In this manner
connections can be shared/re-used among different DataContexts.


>>> 4)  Or am I way off base and making things more complicated than they
>>> should
>>> be? ;-)
>>
>>
>> Probably.  :-)
>
> My thanks to the forum monitors for your patience and explanations!!
>
>>
>> PS. You keep mentioning child contexts.  Do you *need* child contexts?
>
>

Reply via email to