[jboss-user] [JBoss Seam] - EntityManger-per-user-session...
bkyrlach
do-not-reply at jboss.com
Tue Nov 7 11:10:38 EST 2006
Okay...
Everywhere I read, everyone says that this is bad. In fact, it's labeled as an anti-pattern in the Hibernate documentation. However, I still can't help but think that this is an appropriate usage when talking about Seam as the application framework. The only reasons I can find after some searching on Google why this is an anti-pattern is the following...
1) EntityManager is not thread-safe.
Rebuttal: em is not thread safe, but the new feature in Seam 1.1.0 of synchronizing calls to SFSB's means that if you wrap the em in a session scoped seam component, then for our purposes it should be threadsafe.
2) EntityManager uses more memory the longer user session exists because it keeps references to attached objects.
Rebuttal: This is actually true, but doesn't that make sense for a Seam component. I mean using Hibernate without Seam, you have to reattach your entities all the time, because you're presentation layer is always passing id's instead of objects. With Seams contextual bijection, you're always dealing with objects. So, the old way... (assuming updating an already persistent entity)
Activity activity = em.find(Activity.class, activityId);
//update activity
//activity is persisted at the end of the method call because it's
//attached from the finder method.
The new way without using EntityManager-per-user-session...
@In
Activity activity;
...
...
activity = em.merge(activity);
//activity is persisted when you called merge, and was already
//updated because it was injected.
And the way you would do it with the EntityManager-per-session
@In
Activity activity;
...
...
//no code nessecary assuming the updated activity was injected
//and the instance was already attached.
So, please share your thoughts on this. I need to know why EntityManger-per-session is such a bad idea, or if it does truly make sense when dealing with the idea of a Seam component.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3983814#3983814
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3983814
More information about the jboss-user
mailing list