[cdi-dev] [JBoss JIRA] (CDI-520) 3.6. Java EE components

Martin Andersson (JIRA) issues at jboss.org
Sun May 3 05:48:45 EDT 2015


    [ https://issues.jboss.org/browse/CDI-520?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064798#comment-13064798 ] 

Martin Andersson commented on CDI-520:
--------------------------------------

Mark, thank you for your comment. Appreciative that. You wrote:

{quote}
[..] but the EJB and EE umbrella specs only define that this EntityManager works inside an EJB (read section about TransactionManagementType.CONTAINER).
{quote}

I don't agree with this as far as my understanding goes. The JPA specification does not restrict where an entity manager is used.

JPA 2.1, section "7.2.1. Obtaining an Entity Manager in the Java EE Environment":

{quote}
A container-managed entity manager is obtained by the application through dependency injection or through direct lookup of the entity manager in the JNDI namespace. The container manages the persistence context lifecycle and the creation and the closing of the entity manager instance transparently to the application.
{quote}

Nor does the umbrella specification restrict where an entity manager is used, as long as you inject the entity manager into a [managed bean|https://github.com/MartinanderssonDotcom/java-ee-concepts/blob/master/src/test/java/com/martinandersson/javaee/cdi/package-info.java#L68].

Java Platform, Enterprise Edition Specification v7 (JSR-342), section "EE.5.14.1.1 Injection of Persistence Context References":

{quote}
A field or a method of an application component may be annotated with the PersistenceContext annotation.
{quote}

Managed Beans 1.0 (JSR-316), section "MB.2.1.3 Lifecycle and Resource Injection":

{quote}
In a Java EE implementation, a Managed Bean may use any of the resource injection functionality laid out in Chapter EE.5 of the Java EE Platform specification, "Resources, Naming and Injection".
{quote}

Speaking of EJB:s, the EJB specification does not restrict any EJB component type from injecting the entity manager. See EJB 3.2 specification, sections 11.11.1.1., 4.6.2, 4.7.2, 4.8.6, and 5.5.1.

I could not find what section you referred to. I would be a bit shocked if the EJB specification limit any other specification. The persistence context is not even defined by EJB.

Java Platform, Enterprise Edition Specification v7 (JSR-342), section "EE.5 Resources, Naming, and Injection":

{quote}
The PersistenceUnit and PersistenceContext annotations described here are defined in more detail in the Java Persistence specification.
{quote}

You're example of using CDI to look up an _application-managed_ entity manager is a valid one. If all container-managed entity managers share the same entity manager factory (persistence unit), then there are little to no use of using CDI to look up a container-managed entity manager. A too wide CDI scope may in fact expose the entity manager reference to many threads which violate the programming model outlined in JPA. Furthermore, a CDI disposer method must never close a container-managed entity manager. Please read the provided links in my last comment if you want to read my full research on this topic.

> 3.6. Java EE components
> -----------------------
>
>                 Key: CDI-520
>                 URL: https://issues.jboss.org/browse/CDI-520
>             Project: CDI Specification Issues
>          Issue Type: Clarification
>          Components: Java EE integration
>    Affects Versions: 1.1.Final
>            Reporter: Martin Andersson
>             Fix For: TBD
>
>
> I don't understand this text:
> "The instance used by the container to service an invocation of a Java EE component will not be the same instance obtained when using @Inject, instantiated by the container to invoke a producer method, observer method or disposer method, or instantiated by the container to access the value of a producer field."
> More specifically, I am trying to understand how we can use CDI to put a scope on {{EntityManager}} whose life cycle is rather undefined by JPA. I see that the specification use an example of a {{@Disposes}} method to close a container-managed entity manager which throw {{IllegalStateException}} in the disposer (!). Anyways, trying to solve this puzzle has led me to the paragraph quoted previously, of which I understand nothing to be honest.
> My research about the "CDI managed container-managed entity manager" continues. As of now, the example is flawed and a container-managed entity manager remains open after the disposer method. Anyways, I might open up a separate ticket for that.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)


More information about the cdi-dev mailing list