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

Martin Andersson (JIRA) issues at jboss.org
Sat May 2 16:24:45 EDT 2015

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

Martin Andersson commented on CDI-520:

I was trying to understand the life cycle of a container-managed entity manager when using CDI to look it up. My research has been put [here|https://github.com/MartinanderssonDotcom/java-ee-concepts/blob/master/src/test/java/com/martinandersson/javaee/cdi/producers/entitymanager/package-info.java]. As far as my understanding goes, the well spread practice of putting a CDI scope on a container-managed entity manager is not a good thing to do and it contradicts the outspoken goal of container-managed entity managers. Quoting JPA 1.2 specification, section "7.1 Persistence Contexts":

This propagation of persistence contexts by the Java EE container avoids the need for the application to pass references to EntityManager instances from one component to another.

Having CDI pass around the not thread-safe reference for us may expose the entity manager to concurrent calls. Furthermore, many close the entity manager following an example outlined in the CDI specification which is simply wrong because a container-managed entity manager must not be closed by the application. I opened a [ticket|https://issues.jboss.org/browse/CDI-523] for that.

My research has been put [here|https://github.com/MartinanderssonDotcom/java-ee-concepts/blob/master/src/test/java/com/martinandersson/javaee/cdi/producers/entitymanager/package-info.java].

My personal end goal was to know better how to use the entity manager in a Java EE application. This research has been put [here|https://github.com/MartinanderssonDotcom/java-ee-concepts/blob/master/src/test/java/com/martinandersson/javaee/jpa/entitymanagers/package-info.java#L314].

> 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

More information about the cdi-dev mailing list