You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Michael Gentry <mg...@masslight.net> on 2013/02/28 15:43:26 UTC
ServerRuntime Best Practice? [3.1]
Is there an existing place (class) in 3.1 to stuff a ServerRuntime instance
for global use or is it expected that you'll create a separate class to
hold a static instance variable of ServerRuntime?
Thanks,
mrg
Re: ServerRuntime Best Practice? [3.1]
Posted by Ramiro Aparicio <ra...@prot-on.com>.
Hi Michel,
Here is our template: https://gist.github.com/frisco82/5058374
We are using a custom filter to bind a context per request.
And then with our template you have auto rollback with auto rollback on
errors (without losing exception info) and deadlock retries for mysql.
Also this template can be used for async task as if there is not context
bound to thread it just spans one using servlet config (you can change
it to your needs).
All this working with Spring without problems.
We still haven't found the need to use context per session (indeed that
caused a lot of problems also for us) or shorted lived context, but in
that case I would think in using child contexts instead of context.
The template still uses the old semantics of deleteObject/Objects
becouse it was based on another one I found somewhere for Cayenne 2.X
but we have been using it for several months and it is a great
improvement over messing with context in almost every method, before
this we didn't use the filter (nor Spring IoC but that's antoher story)
and there were contexts everywhere.
But anyway just try different options until you are sattisfied with one
Cayenne is really flexible so there is always a workaround to do the
same thing.
Ramiro
El 28/02/2013 17:12, Michael Gentry escribió:
> Hi Ramiro,
>
> I typically create web applications, so manually binding a single context
> to the thread isn't really an option. I need to create them on an ad-hoc
> basis for each user, etc.
>
> Thanks,
>
> mrg
>
>
> On Thu, Feb 28, 2013 at 10:28 AM, Ramiro Aparicio <
> ramiro.aparicio@prot-on.com> wrote:
>
>> El 28/02/2013 16:15, John Huss escribió:
>>
>> If it's a servlet app, then the runtime is created in CayenneFilter and
>>> bound to the thread for each request and you access it via
>>>
>>> WebUtil.getCayenneRuntime(**servletContext);
>>>
>>>
>>> Or you can get a context from the injector if you have bound that to a
>>> thread:
>>>
>>> ObjectContext context =
>>> CayenneRuntime.**getThreadInjector().**getInstance(**
>>> ObjectContextFactory.class
>>> ).createContext();
>>>
>>>
>>> If not a servlet app, then yeah, I'd just store it in a static variable or
>>> application instance.
>>>
>>>
>>>
>>> On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mgentry@masslight.net
>>>> wrote:
>>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
>>>> instance
>>>> for global use or is it expected that you'll create a separate class to
>>>> hold a static instance variable of ServerRuntime?
>>>>
>>>> Thanks,
>>>>
>>>> mrg
>>>>
>>>>
>> We avoid storing it as a singleton and use the same concept for non web
>> apps using BaseContext
>> <http://cayenne.apache.org/**docs/3.1/api/org/apache/**
>> cayenne/BaseContext.html<http://cayenne.apache.org/docs/3.1/api/org/apache/cayenne/BaseContext.html>
>>> **BaseContext.**bindThreadObjectContext(**context)||
>> context = BaseContext.**getThreadObjectContext()
>>
>> So you usually just bind the context on startup and don't need to have
>> write your own singleton for that.
>>
>> Indeed we made a DAO Template that works in both scenarios to be able to
>> reuse the dao library.
>>
>>
>>
>>
>>
Re: ServerRuntime Best Practice? [3.1]
Posted by Michael Gentry <mg...@masslight.net>.
Hi Ramiro,
I typically create web applications, so manually binding a single context
to the thread isn't really an option. I need to create them on an ad-hoc
basis for each user, etc.
Thanks,
mrg
On Thu, Feb 28, 2013 at 10:28 AM, Ramiro Aparicio <
ramiro.aparicio@prot-on.com> wrote:
> El 28/02/2013 16:15, John Huss escribió:
>
> If it's a servlet app, then the runtime is created in CayenneFilter and
>> bound to the thread for each request and you access it via
>>
>> WebUtil.getCayenneRuntime(**servletContext);
>>
>>
>> Or you can get a context from the injector if you have bound that to a
>> thread:
>>
>> ObjectContext context =
>> CayenneRuntime.**getThreadInjector().**getInstance(**
>> ObjectContextFactory.class
>> ).createContext();
>>
>>
>> If not a servlet app, then yeah, I'd just store it in a static variable or
>> application instance.
>>
>>
>>
>> On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mgentry@masslight.net
>> >wrote:
>>
>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
>>> instance
>>> for global use or is it expected that you'll create a separate class to
>>> hold a static instance variable of ServerRuntime?
>>>
>>> Thanks,
>>>
>>> mrg
>>>
>>>
> We avoid storing it as a singleton and use the same concept for non web
> apps using BaseContext
> <http://cayenne.apache.org/**docs/3.1/api/org/apache/**
> cayenne/BaseContext.html<http://cayenne.apache.org/docs/3.1/api/org/apache/cayenne/BaseContext.html>
> >**BaseContext.**bindThreadObjectContext(**context)||
> context = BaseContext.**getThreadObjectContext()
>
> So you usually just bind the context on startup and don't need to have
> write your own singleton for that.
>
> Indeed we made a DAO Template that works in both scenarios to be able to
> reuse the dao library.
>
>
>
>
>
Re: ServerRuntime Best Practice? [3.1]
Posted by Ramiro Aparicio <ra...@prot-on.com>.
El 28/02/2013 16:15, John Huss escribió:
> If it's a servlet app, then the runtime is created in CayenneFilter and
> bound to the thread for each request and you access it via
>
> WebUtil.getCayenneRuntime(servletContext);
>
>
> Or you can get a context from the injector if you have bound that to a
> thread:
>
> ObjectContext context =
> CayenneRuntime.getThreadInjector().getInstance(ObjectContextFactory.class
> ).createContext();
>
>
> If not a servlet app, then yeah, I'd just store it in a static variable or
> application instance.
>
>
>
> On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mg...@masslight.net>wrote:
>
>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime instance
>> for global use or is it expected that you'll create a separate class to
>> hold a static instance variable of ServerRuntime?
>>
>> Thanks,
>>
>> mrg
>>
We avoid storing it as a singleton and use the same concept for non web
apps using BaseContext
<http://cayenne.apache.org/docs/3.1/api/org/apache/cayenne/BaseContext.html>BaseContext.bindThreadObjectContext(context)||
context = BaseContext.getThreadObjectContext()
So you usually just bind the context on startup and don't need to have
write your own singleton for that.
Indeed we made a DAO Template that works in both scenarios to be able to
reuse the dao library.
Re: ServerRuntime Best Practice? [3.1]
Posted by Michael Gentry <mg...@masslight.net>.
Hi Tony,
In our case, we were getting extra Cayenne stacks created at runtime. We
have a bunch of queries that are done at start up (Spring is involved here,
which *might* be part of the problem, but we didn't determine the actual
cause), plus custom types that need to be registered. For some reason,
registering the custom types at start up didn't seem to apply to the filter
and we ended up with two Cayenne stacks created -- one with the custom
types and one without. This caused problems.
Also, some parts of the application don't need to have a session (such as
logout), but the filter kept creating them. Sure, we could've made changes
to the URLs used by the filter/application, but ...
I don't like having a global context in which I commit changes because if
the commit fails (say, a column is missing), it corrupts the entire context
for that session. You can never commit changes elsewhere in the
application during that session because your old "corrupted" data is still
hanging around because it never got committed, but will try to go along for
the ride during your next commitChanges().
So, my preferred approach is to use separate contexts to isolate
edits/transactions and not have a global filter-based one. This means each
user session (or even a single web request) can have many separate
contexts, but they are cheap to create and seem to work better (at least
for my scenarios).
mrg
On Thu, Feb 28, 2013 at 11:51 AM, Tony Giaccone <to...@giaccone.org> wrote:
> Michael,
>
> I'm just curious, what issues does the filter cause??
>
> Tony
>
> On Feb 28, 2013, at 11:08 AM, Michael Gentry <mg...@masslight.net>
> wrote:
>
> > Hi John,
> >
> > We try not to use the CayenneFilter (just removed it from an application
> > because it caused more issues than it solved). I thought there might be
> a
> > place in 3.1 to stuff the ServerRuntime you initialized on application
> > startup and I was just missing it, but perhaps I just need to save it off
> > in a static somewhere.
> >
> > Thanks,
> >
> > mrg
> >
> >
> > On Thu, Feb 28, 2013 at 10:15 AM, John Huss <jo...@gmail.com> wrote:
> >
> >> If it's a servlet app, then the runtime is created in CayenneFilter and
> >> bound to the thread for each request and you access it via
> >>
> >> WebUtil.getCayenneRuntime(servletContext);
> >>
> >>
> >> Or you can get a context from the injector if you have bound that to a
> >> thread:
> >>
> >> ObjectContext context =
> >>
> CayenneRuntime.getThreadInjector().getInstance(ObjectContextFactory.class
> >> ).createContext();
> >>
> >>
> >> If not a servlet app, then yeah, I'd just store it in a static variable
> or
> >> application instance.
> >>
> >>
> >>
> >> On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mgentry@masslight.net
> >>> wrote:
> >>
> >>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
> >> instance
> >>> for global use or is it expected that you'll create a separate class to
> >>> hold a static instance variable of ServerRuntime?
> >>>
> >>> Thanks,
> >>>
> >>> mrg
> >>
>
Re: ServerRuntime Best Practice? [3.1]
Posted by Tony Giaccone <to...@giaccone.org>.
Michael,
I'm just curious, what issues does the filter cause??
Tony
On Feb 28, 2013, at 11:08 AM, Michael Gentry <mg...@masslight.net> wrote:
> Hi John,
>
> We try not to use the CayenneFilter (just removed it from an application
> because it caused more issues than it solved). I thought there might be a
> place in 3.1 to stuff the ServerRuntime you initialized on application
> startup and I was just missing it, but perhaps I just need to save it off
> in a static somewhere.
>
> Thanks,
>
> mrg
>
>
> On Thu, Feb 28, 2013 at 10:15 AM, John Huss <jo...@gmail.com> wrote:
>
>> If it's a servlet app, then the runtime is created in CayenneFilter and
>> bound to the thread for each request and you access it via
>>
>> WebUtil.getCayenneRuntime(servletContext);
>>
>>
>> Or you can get a context from the injector if you have bound that to a
>> thread:
>>
>> ObjectContext context =
>> CayenneRuntime.getThreadInjector().getInstance(ObjectContextFactory.class
>> ).createContext();
>>
>>
>> If not a servlet app, then yeah, I'd just store it in a static variable or
>> application instance.
>>
>>
>>
>> On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mgentry@masslight.net
>>> wrote:
>>
>>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
>> instance
>>> for global use or is it expected that you'll create a separate class to
>>> hold a static instance variable of ServerRuntime?
>>>
>>> Thanks,
>>>
>>> mrg
>>
Re: ServerRuntime Best Practice? [3.1]
Posted by Michael Gentry <mg...@masslight.net>.
Hi John,
We try not to use the CayenneFilter (just removed it from an application
because it caused more issues than it solved). I thought there might be a
place in 3.1 to stuff the ServerRuntime you initialized on application
startup and I was just missing it, but perhaps I just need to save it off
in a static somewhere.
Thanks,
mrg
On Thu, Feb 28, 2013 at 10:15 AM, John Huss <jo...@gmail.com> wrote:
> If it's a servlet app, then the runtime is created in CayenneFilter and
> bound to the thread for each request and you access it via
>
> WebUtil.getCayenneRuntime(servletContext);
>
>
> Or you can get a context from the injector if you have bound that to a
> thread:
>
> ObjectContext context =
> CayenneRuntime.getThreadInjector().getInstance(ObjectContextFactory.class
> ).createContext();
>
>
> If not a servlet app, then yeah, I'd just store it in a static variable or
> application instance.
>
>
>
> On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mgentry@masslight.net
> >wrote:
>
> > Is there an existing place (class) in 3.1 to stuff a ServerRuntime
> instance
> > for global use or is it expected that you'll create a separate class to
> > hold a static instance variable of ServerRuntime?
> >
> > Thanks,
> >
> > mrg
> >
>
Re: ServerRuntime Best Practice? [3.1]
Posted by John Huss <jo...@gmail.com>.
If it's a servlet app, then the runtime is created in CayenneFilter and
bound to the thread for each request and you access it via
WebUtil.getCayenneRuntime(servletContext);
Or you can get a context from the injector if you have bound that to a
thread:
ObjectContext context =
CayenneRuntime.getThreadInjector().getInstance(ObjectContextFactory.class
).createContext();
If not a servlet app, then yeah, I'd just store it in a static variable or
application instance.
On Thu, Feb 28, 2013 at 8:43 AM, Michael Gentry <mg...@masslight.net>wrote:
> Is there an existing place (class) in 3.1 to stuff a ServerRuntime instance
> for global use or is it expected that you'll create a separate class to
> hold a static instance variable of ServerRuntime?
>
> Thanks,
>
> mrg
>
Re: ServerRuntime Best Practice? [3.1]
Posted by Andrus Adamchik <an...@objectstyle.org>.
Yeah, it was a deliberate move away from 3.0 Configuration singleton.
On Feb 28, 2013, at 10:26 PM, Michael Gentry <mg...@masslight.net> wrote:
> Thanks Andrus. I just wanted to make sure I wasn't overlooking something
> with the 3.0 -> 3.1 changes.
>
> mrg
>
>
> On Thu, Feb 28, 2013 at 1:20 PM, Andrus Adamchik <an...@objectstyle.org>wrote:
>
>> ServerRuntime is the "root" object of Cayenne. So there's no other place
>> to store it as far as Cayenne is concerned and this is by design.
>>
>> The whole point of the recent Cayenne evolution was to make Cayenne 100%
>> embeddable and manageable in any environment. Introducing a static
>> singleton in Cayenne (not in your code) is contrary to that goal.
>>
>> Andrus
>>
>> On Feb 28, 2013, at 9:10 PM, Michael Gentry <mg...@masslight.net> wrote:
>>
>>> Hi Andrus,
>>>
>>> I thought about using the ServletContext, but that felt a bit "dirty", so
>>> I'll probably create a singleton to store the runtime. I just thought
>>> maybe there was already a place in 3.1 that I didn't see for the most
>>> common case where you only have one runtime to save.
>>>
>>> Thanks,
>>>
>>> mrg
>>>
>>>
>>> On Thu, Feb 28, 2013 at 12:45 PM, Andrus Adamchik <
>> andrus@objectstyle.org>wrote:
>>>
>>>> Either your own singleton, or ServletContext. ServletContext is
>>>> essentially a designated place for app-scoped objects in a webapp.
>>>>
>>>> Of course if you have Tapestry or Spring (that are themselves internally
>>>> attached to ServletContext), you can use those for ServerRuntime access
>> via
>>>> injection.
>>>>
>>>> Andrus
>>>>
>>>> On Feb 28, 2013, at 5:43 PM, Michael Gentry <mg...@masslight.net>
>> wrote:
>>>>
>>>>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
>>>> instance
>>>>> for global use or is it expected that you'll create a separate class to
>>>>> hold a static instance variable of ServerRuntime?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> mrg
>>>>
>>>>
>>
>>
Re: ServerRuntime Best Practice? [3.1]
Posted by Michael Gentry <mg...@masslight.net>.
Thanks Andrus. I just wanted to make sure I wasn't overlooking something
with the 3.0 -> 3.1 changes.
mrg
On Thu, Feb 28, 2013 at 1:20 PM, Andrus Adamchik <an...@objectstyle.org>wrote:
> ServerRuntime is the "root" object of Cayenne. So there's no other place
> to store it as far as Cayenne is concerned and this is by design.
>
> The whole point of the recent Cayenne evolution was to make Cayenne 100%
> embeddable and manageable in any environment. Introducing a static
> singleton in Cayenne (not in your code) is contrary to that goal.
>
> Andrus
>
> On Feb 28, 2013, at 9:10 PM, Michael Gentry <mg...@masslight.net> wrote:
>
> > Hi Andrus,
> >
> > I thought about using the ServletContext, but that felt a bit "dirty", so
> > I'll probably create a singleton to store the runtime. I just thought
> > maybe there was already a place in 3.1 that I didn't see for the most
> > common case where you only have one runtime to save.
> >
> > Thanks,
> >
> > mrg
> >
> >
> > On Thu, Feb 28, 2013 at 12:45 PM, Andrus Adamchik <
> andrus@objectstyle.org>wrote:
> >
> >> Either your own singleton, or ServletContext. ServletContext is
> >> essentially a designated place for app-scoped objects in a webapp.
> >>
> >> Of course if you have Tapestry or Spring (that are themselves internally
> >> attached to ServletContext), you can use those for ServerRuntime access
> via
> >> injection.
> >>
> >> Andrus
> >>
> >> On Feb 28, 2013, at 5:43 PM, Michael Gentry <mg...@masslight.net>
> wrote:
> >>
> >>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
> >> instance
> >>> for global use or is it expected that you'll create a separate class to
> >>> hold a static instance variable of ServerRuntime?
> >>>
> >>> Thanks,
> >>>
> >>> mrg
> >>
> >>
>
>
Re: ServerRuntime Best Practice? [3.1]
Posted by Andrus Adamchik <an...@objectstyle.org>.
ServerRuntime is the "root" object of Cayenne. So there's no other place to store it as far as Cayenne is concerned and this is by design.
The whole point of the recent Cayenne evolution was to make Cayenne 100% embeddable and manageable in any environment. Introducing a static singleton in Cayenne (not in your code) is contrary to that goal.
Andrus
On Feb 28, 2013, at 9:10 PM, Michael Gentry <mg...@masslight.net> wrote:
> Hi Andrus,
>
> I thought about using the ServletContext, but that felt a bit "dirty", so
> I'll probably create a singleton to store the runtime. I just thought
> maybe there was already a place in 3.1 that I didn't see for the most
> common case where you only have one runtime to save.
>
> Thanks,
>
> mrg
>
>
> On Thu, Feb 28, 2013 at 12:45 PM, Andrus Adamchik <an...@objectstyle.org>wrote:
>
>> Either your own singleton, or ServletContext. ServletContext is
>> essentially a designated place for app-scoped objects in a webapp.
>>
>> Of course if you have Tapestry or Spring (that are themselves internally
>> attached to ServletContext), you can use those for ServerRuntime access via
>> injection.
>>
>> Andrus
>>
>> On Feb 28, 2013, at 5:43 PM, Michael Gentry <mg...@masslight.net> wrote:
>>
>>> Is there an existing place (class) in 3.1 to stuff a ServerRuntime
>> instance
>>> for global use or is it expected that you'll create a separate class to
>>> hold a static instance variable of ServerRuntime?
>>>
>>> Thanks,
>>>
>>> mrg
>>
>>
Re: ServerRuntime Best Practice? [3.1]
Posted by Michael Gentry <mg...@masslight.net>.
Hi Andrus,
I thought about using the ServletContext, but that felt a bit "dirty", so
I'll probably create a singleton to store the runtime. I just thought
maybe there was already a place in 3.1 that I didn't see for the most
common case where you only have one runtime to save.
Thanks,
mrg
On Thu, Feb 28, 2013 at 12:45 PM, Andrus Adamchik <an...@objectstyle.org>wrote:
> Either your own singleton, or ServletContext. ServletContext is
> essentially a designated place for app-scoped objects in a webapp.
>
> Of course if you have Tapestry or Spring (that are themselves internally
> attached to ServletContext), you can use those for ServerRuntime access via
> injection.
>
> Andrus
>
> On Feb 28, 2013, at 5:43 PM, Michael Gentry <mg...@masslight.net> wrote:
>
> > Is there an existing place (class) in 3.1 to stuff a ServerRuntime
> instance
> > for global use or is it expected that you'll create a separate class to
> > hold a static instance variable of ServerRuntime?
> >
> > Thanks,
> >
> > mrg
>
>
Re: ServerRuntime Best Practice? [3.1]
Posted by Andrus Adamchik <an...@objectstyle.org>.
Either your own singleton, or ServletContext. ServletContext is essentially a designated place for app-scoped objects in a webapp.
Of course if you have Tapestry or Spring (that are themselves internally attached to ServletContext), you can use those for ServerRuntime access via injection.
Andrus
On Feb 28, 2013, at 5:43 PM, Michael Gentry <mg...@masslight.net> wrote:
> Is there an existing place (class) in 3.1 to stuff a ServerRuntime instance
> for global use or is it expected that you'll create a separate class to
> hold a static instance variable of ServerRuntime?
>
> Thanks,
>
> mrg