Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 337080

Summary: Duplicate Entities
Product: z_Archived Reporter: Bernard Missing name <bht237>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED INVALID QA Contact:
Severity: critical    
Priority: P3 CC: eclipselink.orm-inbox, michael.f.obrien, mitesh.meswani, petr.jiricka
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
URL: http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg05912.html
Whiteboard:
Attachments:
Description Flags
Testcase reproduces bug none

Description Bernard Missing name CLA 2011-02-13 17:41:50 EST
Build Identifier: 2.2.0.v20110202-r8913

I am getting duplicate managed Entities in EntityManager in a single transaction in GlassFish.

After retrieving them from the list of 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 #codes when compared in the debugger.

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.

The scenario is very difficult to deal with - it breaks existing software and limits my programming options substantially. It is scary and frustrating.

This does not happen in 2.1.2.v20101206-r8635
It also happenes in 2.2.0.v20110114-r8831


Reproducible: Always

Steps to Reproduce:
No testcase available at this stage. Please advise if required - will take me time to make one.
Comment 1 Michael OBrien CLA 2011-02-14 09:17:19 EST
Bernard,
  Hi, I have not noticed this running in the latest GlassFish 3.0.1 and 3.1 still in beta.  I have only run container managed @PersistenceContext injection so far and not application managed RESOURCE_LOCAL.
  In your EclipseLink-users forum post you state that JUnit RESOURCE_LOCAL is fine and this occurs on the container.  could you post your persistence.xml and your entities/mappedSuperclasses so we can compare relationships and object hierarchies.

http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg05912.html

thank you
Comment 2 Bernard Missing name CLA 2011-02-16 05:07:46 EST
Created attachment 189078 [details]
Testcase reproduces bug

This testcase includes everything to reproduce the bug except the libraries. Please refer to the included README file for more details.

I am very nervous about the fact that the failure is not reproducible with the included JUnit test that uses -javaagent to enable weaving.

I am very keen to learn from you - any suggestions on improving the code are highly appreciated, especially in the area of the testing.
Comment 3 Michael OBrien CLA 2011-02-16 09:44:27 EST
>I'll take a look at it in the next 24 hours.
I have not mixed container managed @PersistenceContext injection with a JAXB @XmlRegistry factory before.  Your issue may be related to JAXB-JPA integration and how detached entities created via JAXB are merged into the EM - just guessing at this point until I go through your NetBeans code.
Comment 4 Mitesh Meswani CLA 2011-02-21 21:07:20 EST
Thanks for providing a test case that reproduces the issue. Nothing describes give situation like running code :). 


Your test injects a JTA EntityManager into StatelessSessionBean. The method of this bean called is annotated with @TransactionAttribute(value=TransactionAttributeType.NOT_SUPPORTED). This results in no transaction being active while executing the method. 
The spec requires each access of the injected EM to return detached entities[1]. 

This results in your call to em.find() to give a different instance than the one returned by the query.  This of course does not happen from your unit test because the EM there is RESOURCE_LOCAL and the persistence context lasts through your method call. 

I would suggest you to remove the @TransactionAttribute annotation from the method to make your code work as you expect
 

[1] From section 7.6.1 of JPA 2.0 spec
/
7.6.1 Container-managed Transaction-scoped Persistence Context
The application can obtain a container-managed entity manager with transaction-scoped persistence
context bound to the JTA transaction by injection or direct lookup in the JNDI namespace. The persistence
context type for the entity manager is defaulted or defined as PersistenceContext-
Type.TRANSACTION.
A new persistence context begins when the container-managed entity manager is invoked[76] in the
scope of an active JTA transaction, and there is no current persistence context already associated with
the JTA transaction. The persistence context is created and then associated with the JTA transaction.
The persistence context ends when the associated JTA transaction commits or rolls back, and all entities
that were managed by the EntityManager become detached.
If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database
will immediately become detached at the end of the method call.
/
Comment 5 Michael OBrien CLA 2011-02-21 23:39:27 EST
>Mitesh, yes, I agree that a transaction type of NOT_REQUIRED will temporarily suspend any existing transaction.
In the case of this session bean which not EXTENDED - the annotated method will not participate in container managed transactions.
Thank you for triaging this one.
Comment 6 Bernard Missing name CLA 2011-02-22 03:00:47 EST
Mitesh,

Thank you for your explanation, which will help me to avoid this and help me to structure my testcases to match the behavior of the container.
Comment 7 Petr Jiricka CLA 2011-02-22 07:11:31 EST
Did NetBeans generate this code? Or did you write it by hand? In case NetBeans generated incorrect code, it would be great if you could file a bug against NetBeans - thanks.
Comment 8 Bernard Missing name CLA 2011-02-22 11:43:03 EST
NetBeans did not generate any of the persistence related code so it is ok.
Comment 9 Michael OBrien CLA 2011-03-02 17:16:56 EST
>EclipseLink user has same issue solved by changing from the NOT_REQUIRED
transaction type - which suspends the transaction for the life of the method it annotates - to the default REQUIRED.

http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg05961.html
Comment 10 Eclipse Webmaster CLA 2022-06-09 10:34:24 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink