Home » Eclipse Projects » Remote Application Platform (RAP) » JPA EntityManager(Suggestions for managing EntityManagers?)
JPA EntityManager [message #521956] |
Fri, 19 March 2010 08:49  |
Eclipse User |
|
|
|
Hello,
this is not really RAP specific, but since RAP is a little different from traditional web applications (page requests) I decided to ask it anyway.
I think the question has been asked before, but I still couldn't find a good solution.
Does anybody have suggestions on how to manage the JPA2 EntityManagers in an RAP application?
There are some 'standard' patterns for "application managed" EntityManagers, but none of them seems right to me.
1. method scoped
Create and close the EntityManager in the method that uses the EntityManager...but...what about lazy loading of relations..updating entity properties....and in general performance?
2. unit of work
Create the entity manager when you are about to start some complex operation, close it, after the operation is finished.
For complex business operations this really makes sense. But for simple CRUD operations 'unit of work' is basically the same as method scoped, isn't it?
2. request scoped
In a traditional web application that would be the preferred way for me. Create the EntityManager at the page request (if needed) and close it after the page has been processed.
3. view scoped
One EntityManager for every view of the application. I don't like this one, because I don't want the data access layer to know anything about views and nor do i want a view to know anything about entity managers.
4. application scoped
That would be creating an EntityManager when the application starts, and closing it, when the apllication ends. This one cannot be used in RAP applications, because multiple users would share the same EntityManager and try to access it at the same time.
5. session scoped
Create an EntityManager when a user starts a session and assign it to that session. Close the EntityManager, when the session ends (ISessionStore.addSessionStoreListener).
This is also known as Anti-Pattern (I think mainly because of scalability).
But still, this seems the best way to to it in RAP. There won't be any concurrency issues and I can cleanly close the EntityManager in 'beforeDestroy' by registering a SessionStoreListener. Access to the EntityManager would still needed to by synchronized (fast clicking users)
So really: what's wrong with session scoped EntityManagers?
Or what other options do I have? How do you manage your EntityManagers in RAP applications?
To be clear: I am not talking about container managed EntityManagers, I am talking about application managed EntityManagers.
Thanks for your help.
Markus
[Updated on: Sat, 20 March 2010 05:51] by Moderator
|
|
|
Re: JPA EntityManager [message #522147 is a reply to message #521956] |
Sat, 20 March 2010 10:08   |
Eclipse User |
|
|
|
Originally posted by: karsten_voigt.gmx.de
Hi,
I use a quite simple approach. Since RAP is based on Equinox that is an
OSGI implementation I use an OSGI Service for managing the Entity
Manager. On service activation the Entity Manager is created and on
deactivation it will be destroyed. Everyone that requires the Entity
Manager just requests the service. In this way the Entity Manager is
shared across several users.
For me this approach has one big advantage, I can use the JPA caching
features, since I have a lot of data that is shared between different users.
Karsten
M. Herrmann schrieb:
> Hello,
>
> this is not really RAP specific, but since RAP is a little different
> from traditional web applications (page requests) I decided to ask it
> anyway.
> I think the question has been asked before, but I still couldn't find a
> good solution.
>
> Does anybody have suggestions on how to manage the JPA2 EntityManagers
> in an RAP application?
>
> There are some 'standard' patterns for "application managed"
> EntityManagers, but none of them seems right to me.
>
> 1. method scoped
> Create and close the EntityManager in the method that uses the
> EntityManager...but...what about lazy loading of relations..updating
> entity properties....and in general performance?
>
> 2. unit of work
> Create the entity manager when you are about to start some complex
> operation, close it, after the operation is finished.
> For complex business operations this really makes sense. But for simple
> CRUD operations 'unit of work' is basically the same as method scoped,
> isn't it?
>
> 2. request scoped
> In a traditional web application that would be the preferred way for me.
> Create the EntityManager at the page request (if needed) and close it
> after the page has been processed.
>
> 3. view scoped
> One EntityManager for every view of the application. I don't like this
> one, because I don't want the data access layer to know anything about
> views and nor do i want a view to know anything about entity managers.
>
> 4. application scoped
> That would be creating an EntityManager when the application starts, and
> closing it, when the apllication ends. This one cannot be used in RAP
> applications, because multiple users would share the same EntityManager
> and try to access it at the same time.
>
> 5. session scoped
> Create an EntityManager when a user starts a session and assign it to
> that session. Close the EntityManager, when the session ends
> (ISessionStore.addSessionStoreListener).
> This is also known as Anti-Pattern (I think mainly because of scalability).
> But still, this seems the best way to to it in RAP. There won't be any
> concurrency issues and I can cleanly close the EntityManager in
> 'beforeDestroy' by registering a SessionStoreListener. Access to the
> EntityManager would still needed to by synchronized (fast clicking users)
>
> So really: what's wrong with session scoped EntityManagers?
> Or what other options do I have? How do you manage your EntityManagers
> in RAP applications?
>
> To be clear: I am not talking about container managed EntityManagers, I
> am talking about application managed EntityManagers.
>
> Thanks for your help.
>
> Markus
|
|
| | |
Re: JPA EntityManager [message #522318 is a reply to message #521956] |
Mon, 22 March 2010 06:09   |
Eclipse User |
|
|
|
Markus,
please see my comments below.
Regards
Rüdiger
--
Rüdiger Herrmann
http://eclipsesource.com
On 19.03.2010 13:49, M. Herrmann wrote:
> Hello,
>
> this is not really RAP specific, but since RAP is a little different
> from traditional web applications (page requests) I decided to ask it
> anyway.
> I think the question has been asked before, but I still couldn't find a
> good solution.
>
> Does anybody have suggestions on how to manage the JPA2 EntityManagers
> in an RAP application?
>
> There are some 'standard' patterns for "application managed"
> EntityManagers, but none of them seems right to me.
>
> 1. method scoped
> Create and close the EntityManager in the method that uses the
> EntityManager...but...what about lazy loading of relations..updating
> entity properties....and in general performance?
>
> 2. unit of work
> Create the entity manager when you are about to start some complex
> operation, close it, after the operation is finished.
> For complex business operations this really makes sense. But for simple
> CRUD operations 'unit of work' is basically the same as method scoped,
> isn't it?
You could share one EntityManager among several UnitOfWork instances.
>
> 2. request scoped
> In a traditional web application that would be the preferred way for me.
> Create the EntityManager at the page request (if needed) and close it
> after the page has been processed.
My recommendation would be to create an EntityManager lazily and close
it at the end of each request.
Use PhaseListeners to get notified about the different phases of a
request. Closing the EntityManager after the PROCESS-ACTION phase will do.
>
> 3. view scoped
> One EntityManager for every view of the application. I don't like this
> one, because I don't want the data access layer to know anything about
> views and nor do i want a view to know anything about entity managers.
>
> 4. application scoped
> That would be creating an EntityManager when the application starts, and
> closing it, when the apllication ends. This one cannot be used in RAP
> applications, because multiple users would share the same EntityManager
> and try to access it at the same time.
>
> 5. session scoped
> Create an EntityManager when a user starts a session and assign it to
> that session. Close the EntityManager, when the session ends
> (ISessionStore.addSessionStoreListener).
> This is also known as Anti-Pattern (I think mainly because of scalability).
> But still, this seems the best way to to it in RAP. There won't be any
> concurrency issues and I can cleanly close the EntityManager in
> 'beforeDestroy' by registering a SessionStoreListener. Access to the
> EntityManager would still needed to by synchronized (fast clicking users)
>
> So really: what's wrong with session scoped EntityManagers?
> Or what other options do I have? How do you manage your EntityManagers
> in RAP applications?
>
> To be clear: I am not talking about container managed EntityManagers, I
> am talking about application managed EntityManagers.
>
> Thanks for your help.
>
> Markus
|
|
| |
Re: JPA EntityManager [message #527019 is a reply to message #521956] |
Tue, 13 April 2010 13:23  |
Eclipse User |
|
|
|
Originally posted by: stefan.hansel.web.de
<font size=2 face="sans-serif">We are still at JDO :D ... but the ideas
are the same.</font>
<br>
<br><font size=2 face="sans-serif">We use a request scoped PersistenceManager
and as written by Ruediger also use the PhaseListeners to create/destroy
it. </font>
<br><font size=2 face="sans-serif">I guess if you search the newsgroups
you can find some posts by me talking about PersistenceManagers and Spring
integration going a bit more into detail.</font>
<br>
<br><font size=2 face="sans-serif">If you keep the EntitityManager open
until the request ends you can use your business-object with lazy loading
in the view which might safe some time coding.</font>
<br><font size=2 face="sans-serif">If you need remote API you should go
for method-based EM as this luxury of Lazy-Loading doesn't work remote.</font>
<br>
<br><font size=2 face="sans-serif">One legacy (Swing-)-App which we need
to port some time in the future will use SessionBased PersistenceManagers.
With this you need to be very aware of the rules of the First-Level-Cache
is this one can get very nasty in long running applications.</font>
<br><font size=2 face="sans-serif">Be aware that this will increase memory
consumptions. In our case we plan for <50 users so this will be ok.
Greate 50 I wouldn't go for session-based, due to the EntityManagerCache.</font>
<br>
<br>
<br>
|
|
|
Goto Forum:
Current Time: Wed Jul 23 15:24:30 EDT 2025
Powered by FUDForum. Page generated in 0.07620 seconds
|