You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Christian Grobmeier <gr...@gmail.com> on 2011/02/16 10:50:16 UTC

ObjectContext per thread or per app?

Hello,

sorry, a dumb question:

do I need a ObjectContext per thread or per app?

In fact I wrote a spring bean (its a singleton) which creates one
single ObjectContext for all my app. In other terms, multiple threads
will use it to insert, read, commit etc.
Since ObjectContext offers the commit method, I am now doubting if
this was correct. I feel it might be good to have an ObjectContext for
each thread/user. Or does the ObjectContext know what happened as one
transaction?

Can you advise?

Thanks
Christian

-- 
http://www.grobmeier.de

Re: ObjectContext per thread or per app?

Posted by Andrus Adamchik <an...@objectstyle.org>.
My current favorite pattern is a single read-only context per app (this gives very good read performance, especially when combined with LOCAL_CACHE cache strategy), and short-scoped per user contexts for editing (short scoped can mean different things in different apps I guess, depending on whether you need to carry over uncommitted state between requests, etc.)

Andrus

On Feb 16, 2011, at 4:17 PM, Michael Gentry wrote:
> Hi Christian,
> 
> You'll typically have at least one context-per-user.  (Not always, but
> typically.)  The patterns we use here are to have a thread-based
> context-per-user and also separate (peer or child) contexts on our
> edit pages.  This lets us isolate the edit changes in a different
> context (and wire the UI directly to the underlying Cayenne object)
> and we are able to throw the context away without polluting the main
> thread-based context when needed (such as when there are validation
> errors and the user cancels instead of fixing them).  This approach
> keeps unexpected modified objects from floating around in our
> thread-based context that might later get committed when we didn't
> want them.
> 
> mrg
> 
> 
> On Wed, Feb 16, 2011 at 5:18 AM, Christian Grobmeier
> <gr...@gmail.com> wrote:
>> Thanks Andrey,
>> this helped.
>> Christian
>> 
>> On Wed, Feb 16, 2011 at 10:56 AM, Andrey Razumovsky
>> <ra...@gmail.com> wrote:
>>> Hi Christian,
>>> 
>>> In case of Web application it is incorrect to have one ObjectContext,
>>> because you can't really know which session added certain changes, so
>>> you'll be facing tons of multithreading issues, e.g. one session will
>>> commit other session's partial changes. Common way is to have one
>>> context per session, and for easy access to it having it bound to your
>>> servlet's thread. See
>>> http://cayenne.apache.org/doc30/web-applications.html
>>> 
>>> Hope that helps,
>>> Andrey
>>> 
>>> 2011/2/16 Christian Grobmeier <gr...@gmail.com>:
>>>> Hello,
>>>> 
>>>> sorry, a dumb question:
>>>> 
>>>> do I need a ObjectContext per thread or per app?
>>>> 
>>>> In fact I wrote a spring bean (its a singleton) which creates one
>>>> single ObjectContext for all my app. In other terms, multiple threads
>>>> will use it to insert, read, commit etc.
>>>> Since ObjectContext offers the commit method, I am now doubting if
>>>> this was correct. I feel it might be good to have an ObjectContext for
>>>> each thread/user. Or does the ObjectContext know what happened as one
>>>> transaction?
>>>> 
>>>> Can you advise?
>>>> 
>>>> Thanks
>>>> Christian
>>>> 
>>>> --
>>>> http://www.grobmeier.de
>>>> 
>>> 
>>> 
>>> 
>>> --
>>> Andrey
>>> 
>> 
>> 
>> 
>> --
>> http://www.grobmeier.de
>> 
> 


Re: ObjectContext per thread or per app?

Posted by Michael Gentry <mg...@masslight.net>.
Hi Christian,

You'll typically have at least one context-per-user.  (Not always, but
typically.)  The patterns we use here are to have a thread-based
context-per-user and also separate (peer or child) contexts on our
edit pages.  This lets us isolate the edit changes in a different
context (and wire the UI directly to the underlying Cayenne object)
and we are able to throw the context away without polluting the main
thread-based context when needed (such as when there are validation
errors and the user cancels instead of fixing them).  This approach
keeps unexpected modified objects from floating around in our
thread-based context that might later get committed when we didn't
want them.

mrg


On Wed, Feb 16, 2011 at 5:18 AM, Christian Grobmeier
<gr...@gmail.com> wrote:
> Thanks Andrey,
> this helped.
> Christian
>
> On Wed, Feb 16, 2011 at 10:56 AM, Andrey Razumovsky
> <ra...@gmail.com> wrote:
>> Hi Christian,
>>
>> In case of Web application it is incorrect to have one ObjectContext,
>> because you can't really know which session added certain changes, so
>> you'll be facing tons of multithreading issues, e.g. one session will
>> commit other session's partial changes. Common way is to have one
>> context per session, and for easy access to it having it bound to your
>> servlet's thread. See
>> http://cayenne.apache.org/doc30/web-applications.html
>>
>> Hope that helps,
>> Andrey
>>
>> 2011/2/16 Christian Grobmeier <gr...@gmail.com>:
>>> Hello,
>>>
>>> sorry, a dumb question:
>>>
>>> do I need a ObjectContext per thread or per app?
>>>
>>> In fact I wrote a spring bean (its a singleton) which creates one
>>> single ObjectContext for all my app. In other terms, multiple threads
>>> will use it to insert, read, commit etc.
>>> Since ObjectContext offers the commit method, I am now doubting if
>>> this was correct. I feel it might be good to have an ObjectContext for
>>> each thread/user. Or does the ObjectContext know what happened as one
>>> transaction?
>>>
>>> Can you advise?
>>>
>>> Thanks
>>> Christian
>>>
>>> --
>>> http://www.grobmeier.de
>>>
>>
>>
>>
>> --
>> Andrey
>>
>
>
>
> --
> http://www.grobmeier.de
>

Re: ObjectContext per thread or per app?

Posted by Christian Grobmeier <gr...@gmail.com>.
Thanks Andrey,
this helped.
Christian

On Wed, Feb 16, 2011 at 10:56 AM, Andrey Razumovsky
<ra...@gmail.com> wrote:
> Hi Christian,
>
> In case of Web application it is incorrect to have one ObjectContext,
> because you can't really know which session added certain changes, so
> you'll be facing tons of multithreading issues, e.g. one session will
> commit other session's partial changes. Common way is to have one
> context per session, and for easy access to it having it bound to your
> servlet's thread. See
> http://cayenne.apache.org/doc30/web-applications.html
>
> Hope that helps,
> Andrey
>
> 2011/2/16 Christian Grobmeier <gr...@gmail.com>:
>> Hello,
>>
>> sorry, a dumb question:
>>
>> do I need a ObjectContext per thread or per app?
>>
>> In fact I wrote a spring bean (its a singleton) which creates one
>> single ObjectContext for all my app. In other terms, multiple threads
>> will use it to insert, read, commit etc.
>> Since ObjectContext offers the commit method, I am now doubting if
>> this was correct. I feel it might be good to have an ObjectContext for
>> each thread/user. Or does the ObjectContext know what happened as one
>> transaction?
>>
>> Can you advise?
>>
>> Thanks
>> Christian
>>
>> --
>> http://www.grobmeier.de
>>
>
>
>
> --
> Andrey
>



-- 
http://www.grobmeier.de

Re: ObjectContext per thread or per app?

Posted by Andrey Razumovsky <ra...@gmail.com>.
Hi Christian,

In case of Web application it is incorrect to have one ObjectContext,
because you can't really know which session added certain changes, so
you'll be facing tons of multithreading issues, e.g. one session will
commit other session's partial changes. Common way is to have one
context per session, and for easy access to it having it bound to your
servlet's thread. See
http://cayenne.apache.org/doc30/web-applications.html

Hope that helps,
Andrey

2011/2/16 Christian Grobmeier <gr...@gmail.com>:
> Hello,
>
> sorry, a dumb question:
>
> do I need a ObjectContext per thread or per app?
>
> In fact I wrote a spring bean (its a singleton) which creates one
> single ObjectContext for all my app. In other terms, multiple threads
> will use it to insert, read, commit etc.
> Since ObjectContext offers the commit method, I am now doubting if
> this was correct. I feel it might be good to have an ObjectContext for
> each thread/user. Or does the ObjectContext know what happened as one
> transaction?
>
> Can you advise?
>
> Thanks
> Christian
>
> --
> http://www.grobmeier.de
>



-- 
Andrey