Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: AW: [eclipselink-users] When are Entities Equal?

In general I would recommend you create a new EntityManager per server
request, or per transaction.

If you are using a JEE managed EntityManager one is automatically created
per transaction, or per operation outside a transaction.

I suppose it is related to having a stateful architecture or a stateless
architecture.  If you have a stateless architecture than you would create a
new EntityManager per request.  If you have a stateful architecture that you
could create one per user session, but be careful of user sessions living
too long.

In general the cost of creating an EntityManager is pretty minimal, and the
main concern with a long lived EntityManager is that the entities that it
has registered may become out of date.

In EclipseLink an EntityManager only holds a connections from the pool in a
transaction after a flush or write, or if an exclusive connection policy is
used (VPD).


Alex_S wrote:
> 
> Hi,
> 
> I have a question about when to work with the same EntityManager and when
> to close it and get a new one.
> The JPA examples I found where quite different, sometimes the
> EntityManager is created and used the whole time in the example, and
> sometimes for each query a new EntityManager is created (like in the
> TopLink Essentials examples
> http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html
> ).
> An EntityManager is not threadsafe, ok, but what if I use it sequentially?
> For example:
> If I have a Web Application is it better to create an EntityManager for
> every User Session (there wont be many), reuse it for every read operation
> and only create other EntityManager if I need to read something
> simultaneously, or is it better to create/close an EntityManager in every
> read/write method?
> 
> Thanks in advance, best regards,
> Alex Schaefer
> 
> -------- Original-Nachricht --------
>> Datum: Tue, 15 Apr 2008 20:14:32 -0400
>> Von: Gordon Yorke <gordon.yorke@xxxxxxxxxx>
>> An: EclipseLink User Discussions <eclipselink-users@xxxxxxxxxxx>
>> Betreff: Re: [eclipselink-users] When are Entities Equal?
>> Hello Tim,
>>    Yes, as long as you continue to work within the same EntityManager 
>> without clearing then only one instance of any object will exist within 
>> that EntityManager/persistence context.  em.find(Employee.class, 
>> managerId) == department.getManager().  Unless you are in an Application 
>> Server you control when the EntityManager is closed or cleared. 
>> Container managed transactional entity managers will get switched at the 
>> end of the transaction.  Once you call clear or act on a different 
>> EntityManager then the instances from the old EntityManager will not be 
>> the same instances as retrieved from the new EntityManager.
>> --Gordon
>> 
>> Tim Hollosy wrote:
>> > I've got an entity with a many to one, let's call it employees, with a
>> > department.
>> >
>> > So my employee entity has a Department field.
>> >
>> > If I'm in the same Entity Manager and I do a find on an Employee to
>> > get an instance of a specific Employee Entity, then later in the same
>> > EM I do a select all on departments, my list of departments now
>> > contains a reference to the same Department that my Employee entity
>> > references.
>> >
>> > This is really great for doing things like databinding on drop down
>> > lists, because I can bind the list of departments directly to the
>> > department on my employee entity.
>> >
>> > However, is this safe? Can I count on these being the _same_ entities;
>> > not just entities with the same field values all the time? What's
>> > eclipselink doing behind the scenes?
>> >
>> > Thanks,
>> > Tim
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/AW%3A-When-are-Entities-Equal--tp16873274p16938487.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top