Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » JPA EntityManager(Suggestions for managing EntityManagers?)
icon5.gif  JPA EntityManager [message #521956] Fri, 19 March 2010 12:49 Go to next message
M. Herrmann is currently offline M. HerrmannFriend
Messages: 25
Registered: March 2010
Junior Member
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 09:51]

Report message to a moderator

Re: JPA EntityManager [message #522147 is a reply to message #521956] Sat, 20 March 2010 14:08 Go to previous messageGo to next message
Eclipse UserFriend
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 #522149 is a reply to message #522147] Sat, 20 March 2010 14:37 Go to previous messageGo to next message
M. Herrmann is currently offline M. HerrmannFriend
Messages: 25
Registered: March 2010
Junior Member
Thanks for your reply. Interesting.

The one thing I ask myself looking at your approach is:
What about concurrency? What if the EntityManager is in the middle of a transaction and another user requests the EntityManager (by clicking on something that loads or writes data)?

Markus


[quote title=Eclipse User wrote on Sat, 20 March 2010 10:08]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
Re: JPA EntityManager [message #522176 is a reply to message #522149] Sat, 20 March 2010 19:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: karsten_voigt.gmx.de

You're right; concurrency might an issue. In my current case I use
several EntityManagers. Most of my applications actions are read actions
for the several tables (I guess about 80%), which use a same
EntityManager. For actions that require a transaction, a new
EntityManager is created (within the OSGI Service) for each transaction.
I also though about a simple EntityManager pooling, but in my case that
doesn't make so much sense...

I'm a friend of separation of aspects and the User Interface (RAP) shall
not know anything about persistence. The only thing RAP knows is that
there is a service that can handle this stuff. (For example I have some
data in a LDAP and some in the database; for my RAP application its
treated in the same way).

If my application grows I'll think about container managed
EntityManagers, but still using the OSGI service.

Karsten


M. Herrmann schrieb:
> Thanks for your reply. Interesting.
>
> The one thing I ask myself looking at your approach is:
> What about concurrency? What if the EntityManager is in the middle of a
> transaction and another user requests the EntityManager (by clicking on
> something that loads or writes data)?
> Markus
>
>
> [quote title=Eclipse User wrote on Sat, 20 March 2010 10:08]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
Re: JPA EntityManager [message #522318 is a reply to message #521956] Mon, 22 March 2010 10:09 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger HerrmannFriend
Messages: 581
Registered: July 2009
Senior Member
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 #522497 is a reply to message #522318] Mon, 22 March 2010 19:21 Go to previous messageGo to next message
M. Herrmann is currently offline M. HerrmannFriend
Messages: 25
Registered: March 2010
Junior Member
Rüdiger Herrmann wrote on Mon, 22 March 2010 06:09

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.



Thanks Rüdiger,

I didn't even think about PhaseListeners but that sounds good.

Greetings from Böblingen, Germany
Re: JPA EntityManager [message #527019 is a reply to message #521956] Tue, 13 April 2010 17:23 Go to previous message
Eclipse UserFriend
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 &lt;50 users so this will be ok.
Greate 50 I wouldn't go for session-based, due to the EntityManagerCache.</font>
<br>
<br>
<br>
Previous Topic:Request.js - eval not in global scope for IE
Next Topic:[ANN] RAP 1.3 M6 is available
Goto Forum:
  


Current Time: Fri Apr 19 11:55:54 GMT 2024

Powered by FUDForum. Page generated in 0.02374 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top