users@glassfish.java.net

CDI vs resource injection

From: <glassfish_at_javadesktop.org>
Date: Thu, 04 Mar 2010 04:04:28 PST

Hi.

I have 2 stateless beans with transaction defaults (container managed, REQUIRED), both have 2 EntityManagers injected: TestBeanImplPlain uses @PersistenceContext, whereas TestBeanImplCDI uses @Inject, and there also exists a EntityManagerProducer that uses @PersistenceContext @Produces field, as outlined in the CDI specs.
I noticed different behaviour - the EntityManager injected via @PersistetContext (the 'old' way), have different identities; i.e., they are actually wrappers (instances of com.sun.enterprise.container.common.impl.EntityManagerWrapper) , but different instances, that wrap around the same instance of real EclipseLink's EntityManagerImpl (and thus sharing the persistence context in the same container transaction). That is compliant with the specs, which says that 'normally' each JNDI lookup / resource injection returns a new instance unless immutable or the application cannot change the state of such object, or it is a singleton (EE 5.2.4).

However, CDI injection behaves differently - both injected wrappers are the very same instance of com.sun.enterprise.container.common.impl.EntityManagerWrapper, which also wrap around the same underlying EclipseLInk's EntityManagerImpl. So, the context is shared in the same container transaction, but I am not sure how big an impact the fact that the wrappers are the same instances might have. I also am not sure if this complies with the specs.

Could someone more experienced in the field explain me if this is correct behavior (to inject the same wrappers) or not, and what impact it might have?

I am using GlassFish v3 (build 74.2). I can provide a project that has a servlet with those beans injected, and prints out information about the injected resources on each invocation.

Regards,
szczyp
[Message sent by forum member 'szczyp' (szczyp_at_gmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=390020