Hi Andrew, I'm coming to the party late, but a few quick comments inline.

mrg

On Mon, Apr 30, 2012 at 1:36 PM, Andrew Willerding
<[email protected]> wrote:
> I have built a few Web apps using Cayenne quite successfully but now I am
> building my first standalone app and have a few questions.
>
> In a Web app, I have typically just setup my Tomcat configuration and in my
> code I execute something like ObjectContext context =
> BaseContext.getThreadObjectContext() and let the "magic" happen.
>
> In my standalone application I don't think it will be quite that easy.  I
> will have multiple short lived threads requesting database information
> simultaneously so I'm not quite sure how to manage the Cayenne components.
>  At first I thought I could simple do a DataContext.createDataContext() and
> I'd be fine but "Creating a new DataContext for each request is not a
> recommended practice."  So I understand the need for a single DataContext
> and so I think I found the answer in createChildContext() but,


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.


> 1)  Do I need to manually create a child context in each thread?  But what
> should I do when the thread has completed its transactions and is ready to
> terminate?  I don't see a corresponding "destroyChildContext"
>
>            ObjectContext oc = getDataContext().createChildContext();


This is more of a design decision (sorry to be fuzzy).  If it makes
sense to you to create a child context for your requirements, do it.
As for destroying a child context, all DataContexts are free to be
garbage collected if you don't hold onto them.


> 2)  Or is it better to create a pool of ObjectContexts that I have to
> manually manage access to within the threads - or does Cayenne have
> something like this already built-in?


You typically don't care to create a pool of re-usable contexts.
There are some exceptions to this for read-mostly data, though.


> 3)  Is the maximum simultaneous createChildContext calls  tied to the
> ConnectionPool limits in the Modeler?  To me the "createChildContext"
> implies creation without limit so if they are somehow linked how will I know
> that the underlying ConnectionPool has run out of resources?


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.


> 4)  Or am I way off base and making things more complicated than they should
> be? ;-)


Probably.  :-)

PS. You keep mentioning child contexts.  Do you *need* child contexts?

Reply via email to