12 Replies Latest reply on Mar 15, 2012 5:04 PM by Karsten Wutzke

    AS 7.1 allowing initiation of a container-managed extended persistence context inside a stateless bean

    Karsten Wutzke Expert

      Hello,

       

      I'm using a container-managed EXTENDED persistence context from within a stateless bean like (abstract base class):

      {code}@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

      public abstract class JpaCrudService implements CrudService

      {

          private static final Logger log = LoggerFactory.getLogger(JpaCrudService.class);

       

          @PersistenceContext(unitName="MyPu", type=PersistenceContextType.EXTENDED)

          protected EntityManager em;

       

          ...

      }{code}

      then:

      {code}@Stateless

      public class UserService extends JpaCrudService

      {

          ...

      }{code}

      On JBoss AS 7.1 Final this works fine, but on GlassFish 3.1.2 the following exception is thrown:

      {code}    at org.glassfish.faces.integration.GlassFishInjectionProvider$2.run(GlassFishInjectionProvider.java:382)

          ... 94 more

      Caused by: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB

          at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)

          at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)

          at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)

          at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)

          at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)

          at $Proxy212.findAllUsers(Unknown Source)

          at de.poyry.pqgenerator.service.__EJB31_Generated__UserService__Intf____Bean__.findAllUsers(Unknown Source)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

          at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:264)

          at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)

          at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137)

          at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:260)

          at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:111)

          at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)

          at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105)

          at de.poyry.pqgenerator.service.UserService$Proxy$_$$_Weld$Proxy$.findAllUsers(UserService$Proxy$_$$_Weld$Proxy$.java)

          at de.poyry.pqgenerator.beans.UserManager.getUsers(UserManager.java:91)

          at de.poyry.pqgenerator.beans.UserManager.getDefaultUser(UserManager.java:84)

          at de.poyry.pqgenerator.beans.UserManager.getCurrentUser(UserManager.java:43)

          at de.poyry.pqgenerator.beans.UserManager$Proxy$_$$_WeldClientProxy.getCurrentUser(UserManager$Proxy$_$$_WeldClientProxy.java)

          at de.poyry.pqgenerator.beans.PqManager.init(PqManager.java:75)

          ... 99 more

      Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB

          at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)

          at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)

          at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)

          ... 122 more

      Caused by: javax.ejb.CreateException: Could not create stateless EJB

          ...

          ...

          ...

          ... 134 more

      Caused by: javax.naming.NameNotFoundException: No object bound for java:comp/env/de.poyry.pqgenerator.service.JpaCrudService/em [Root exception is java.lang.IllegalStateException: EntityManager with PersistenceContextType.EXTENDED is not supported for this bean type]

          at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:239)

          at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)

          ... 138 more

      Caused by: java.lang.IllegalStateException: EntityManager with PersistenceContextType.EXTENDED is not supported for this bean type

          at com.sun.ejb.containers.BaseContainer.lookupExtendedEntityManager(BaseContainer.java:1979)

          at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:235)

          at com.sun.enterprise.container.common.impl.EntityManagerWrapper.initializeEMWrapper(EntityManagerWrapper.java:122)

          at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$FactoryForEntityManagerWrapper.create(ComponentEnvManagerImpl.java:896)

          at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776)

          at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744)

          at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169)

          ... 139 more{code}

      Checking the JPA 2 spec clearly states (section 7.6.2):

       

      "A container-managed extended persistence context can only be initiated within the scope of a stateful session bean." (although it should probably read "... stateful bean" only)

       

      In that respect, GlassFish seems to be right to not allow this. Is that so? Can anyone confirm this?

       

      Why does AS 7 initiate this? Isn't this a bug?

       

      Karsten