jpa-spec
  1. jpa-spec
  2. JPA_SPEC-3

Allow components other than stateful sessions beans to initiate container-managed extended persistence contexts

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      None

      Description

      It might be useful to support the use of container-managed extended persistence contexts in singleton session beans and/or application-scoped or conversation-scoped CDI components.

        Activity

        Hide
        donatasc added a comment -

        +100

        IMHO this was a huge miss in Java EE 6. Even though CDI introduced conversation scope, injection/creation of extended persistence context EM was not mentioned at all. EJB spec is also silent about extended PC, what caused me to post message like this to JPA mailing list:
        http://java.net/projects/jpa-spec/lists/users/archive/2013-01/message/51

        In general, extended persistence context is largely under-specified in regards to integration between CDI/EJB/JPA.

        Show
        donatasc added a comment - +100 IMHO this was a huge miss in Java EE 6. Even though CDI introduced conversation scope, injection/creation of extended persistence context EM was not mentioned at all. EJB spec is also silent about extended PC, what caused me to post message like this to JPA mailing list: http://java.net/projects/jpa-spec/lists/users/archive/2013-01/message/51 In general, extended persistence context is largely under-specified in regards to integration between CDI/EJB/JPA.
        Hide
        kostd added a comment - - edited

        In fact it is working now. I think before (on java 6) its already worked, but not sure.

        Checked in environments:

        Environment1: java7u80, wildfly 8.2.0.Final, wildfly-jpa-8.2.0.Final, hibernate 4.3.7.Final, weld 2.2.6.Final, wildfly-ejb3 8.2.0.Final, extended-persistence inheritance = DEEP(default)
        Environment2: java8u77, wildfly 10.0.0.Final, wildfly-jpa-10.0.0.Final, hibernate 5.0.7.Final, weld 2.3.2.Final, wildfly-ejb3 10.0.0.Final, extended-persistence inheritance = DEEP(default)

        have:

        
        @ConversationScoped
        public class CDIBean1 implements Serializable{
        
        	private static final long serialVersionUID = 1L;
        
        	@Inject
        	CDIBean2 cdibean2;
        	
        	@Inject
        	StatelessBean statelessBean;
        	
        	public void doSomthing(){
        		
        		cdibean2.doSomthing();
        		statelessBean.doSomthing();
        		
        	}
        }
        ...
        
        @ConversationScoped
        public class CDIBean2  implements Serializable{
        
        	private static final long serialVersionUID = 1L;
        
        	@PersistenceContext(type=PersistenceContextType.EXTENDED, synchronization=SynchronizationType.UNSYNCHRONIZED)
        	EntityManager em;
        	
        	
        	public void doSomthing(){
        		
        		List<StreetType> streets  = em.createQuery(" from StreetType").getResultList();
        		
        		SessionImpl session = (SessionImpl) em.unwrap(SessionImpl.class);
        		System.out.println("there are " + session.getPersistenceContext().getNumberOfManagedEntities() + " entities in XPC after initiation");
        	}
        }
        ...
        
        @Stateless
        @TransactionManagement(TransactionManagementType.CONTAINER)
        public class StatelessBean {
        	
        	@PersistenceContext
        	EntityManager em;
        	
        	public void doSomthing(){
        		
        		SessionImpl session = (SessionImpl) em.unwrap(SessionImpl.class);
        		
        		System.out.println("there are " + session.getPersistenceContext().getNumberOfManagedEntities() + " entities in transaction-scoped persistence context");
        		
        	}
        
        }
        
        

        after call CDIBean1#doSomething() can see that XPC was initiated in CDIBean2#doSomething() and propogated as TxPC in stateless EJB. Both output strings show same managed object count in persistence context.

        I vote to reviewing of "7.6.3 Container-managed Extended Persistence Context" in JPA-SPEC 2.1:

        A container-managed extended persistence context can only be initiated within the scope of a stateful
        session bean. It exists from the point at which the stateful session bean that declares a dependency on an
        entity manager of type PersistenceContextType.EXTENDEDis created, and is said to be bound
        to the stateful session bean.

        this is not truth truth is:

        A container-managed extended persistence context can only be initiated within the scope of a stateful
        session bean or CDI bean. It exists from the point at which the bean that declares a dependency on an
        entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound
        to that (stateful or CDI) bean.

        Show
        kostd added a comment - - edited In fact it is working now. I think before (on java 6) its already worked, but not sure. Checked in environments: Environment1: java7u80, wildfly 8.2.0.Final, wildfly-jpa-8.2.0.Final, hibernate 4.3.7.Final, weld 2.2.6.Final, wildfly-ejb3 8.2.0.Final, extended-persistence inheritance = DEEP(default) Environment2: java8u77, wildfly 10.0.0.Final, wildfly-jpa-10.0.0.Final, hibernate 5.0.7.Final, weld 2.3.2.Final, wildfly-ejb3 10.0.0.Final, extended-persistence inheritance = DEEP(default) have: @ConversationScoped public class CDIBean1 implements Serializable{ private static final long serialVersionUID = 1L; @Inject CDIBean2 cdibean2; @Inject StatelessBean statelessBean; public void doSomthing(){ cdibean2.doSomthing(); statelessBean.doSomthing(); } } ... @ConversationScoped public class CDIBean2 implements Serializable{ private static final long serialVersionUID = 1L; @PersistenceContext(type=PersistenceContextType.EXTENDED, synchronization=SynchronizationType.UNSYNCHRONIZED) EntityManager em; public void doSomthing(){ List<StreetType> streets = em.createQuery( " from StreetType" ).getResultList(); SessionImpl session = (SessionImpl) em.unwrap(SessionImpl.class); System .out.println( "there are " + session.getPersistenceContext().getNumberOfManagedEntities() + " entities in XPC after initiation" ); } } ... @Stateless @TransactionManagement(TransactionManagementType.CONTAINER) public class StatelessBean { @PersistenceContext EntityManager em; public void doSomthing(){ SessionImpl session = (SessionImpl) em.unwrap(SessionImpl.class); System .out.println( "there are " + session.getPersistenceContext().getNumberOfManagedEntities() + " entities in transaction-scoped persistence context" ); } } after call CDIBean1#doSomething() can see that XPC was initiated in CDIBean2#doSomething() and propogated as TxPC in stateless EJB. Both output strings show same managed object count in persistence context. I vote to reviewing of "7.6.3 Container-managed Extended Persistence Context" in JPA-SPEC 2.1: A container-managed extended persistence context can only be initiated within the scope of a stateful session bean. It exists from the point at which the stateful session bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDEDis created, and is said to be bound to the stateful session bean. this is not truth truth is: A container-managed extended persistence context can only be initiated within the scope of a stateful session bean or CDI bean. It exists from the point at which the bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound to that (stateful or CDI) bean.

          People

          • Assignee:
            ldemichiel
            Reporter:
            ldemichiel
          • Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: