Within a JTA transaction the EntityManager must always return the same
instance (unless you call clear).
Are you sure it is a single transaction? Or is it two different
transactions, or not in a JTA transaction context? With a JTA managed
EntityManager a different instance must be return across each transaction
boundary.
Ensure you are in a single transaction and are not calling clear, perhaps
enable logging on finest.
bht wrote:
Hi,
I am getting duplicate managed Entities in EntityManager in a single
transaction.
This happens only in GlassFish, not with a JUnit test that uses
transaction-type="RESOURCE_LOCAL" in persistence.xml.
After retrieveal from an object query, the same entities are looked up
again using EntityManager.find().
find() gets the duplicates. I spotted them because they "lost" some
transient fields that were set prior to find(), and they have
different Object hashcodes.
One could argue that there is no contract that stipulates that
EntityManager must return identical entities for the same entity type
and ID within the same transaction.
However, there is also no reason for such a waste of memory because if
I can compare them, then I have the references to them and they cannot
be garbage collected anyway.
Is this a bug? Should I report this?
The scenario is very difficult to deal with - it limits my programming
options quite a bit. It is scary and frustrating.
Regards,
Bernard
-----
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
Blog: http://java-persistence-performance.blogspot.com/ Java Persistence
Performance