9 Replies Latest reply on Jan 16, 2010 6:57 PM by Pete Muir

    CDI injected EntityManager into @ConversationScoped SFSB.

    Justin Wyer Newbie

      Having a bit of trouble using an @Produces CDI injected EntityManager. This is a pretty simple EE6 web app been deployed as a war on Glassfish v3. I think its probably a case of me just doing something wrong, but I can't for the life of me figure out what.


      I have a @ConversationScoped (if I use @SessionScoped the problem does not present) stateful EJB




      @Stateful
      @ConversationScoped
      @Named
      public class AccountController





      into which I am injecting the container managed EntityManager it works fine if I inject it with @PersistenceContext


      I wrote a producer to try some type safe injection of the EntityManager.




      public class AccountDatabaseProducer implements Serializable {
      
          @Produces
          @PersistenceContext
          @AccountDatabase
          private EntityManager em;
      
      }





      However when I inject the EntityManager using @Inject @AccountDatabase it works but this exception shows up on the log file (If I @ConversationScoped the EntityManager in AccountDatabaseProducer this exception is caught by JSF and thrown to the page)







      javax.ejb.EJBException: java.lang.IllegalStateException
              at com.sun.ejb.containers.StatefulSessionContainer.removeBean(StatefulSessionContainer.java:1043)
              at com.sun.ejb.containers.StatefulSessionContainer.removeBean(StatefulSessionContainer.java:961)
              at com.sun.ejb.containers.EJBLocalObjectImpl.remove(EJBLocalObjectImpl.java:179)
              at com.sun.ejb.containers.EjbContainerServicesImpl.remove(EjbContainerServicesImpl.java:144)
              at org.glassfish.weld.ejb.SessionObjectReferenceImpl.remove(SessionObjectReferenceImpl.java:66)
              at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:106)
              at com.voxtelecom.fireid.controller.AccountController_$$_javassist_10.destroy(AccountController_$$_javassist_10.java)
              at org.jboss.weld.bean.SessionBean.destroy(SessionBean.java:302)
              at org.jboss.weld.context.ForwardingContextual.destroy(ForwardingContextual.java:18)
              at org.jboss.weld.context.AbstractMapContext.destroy(AbstractMapContext.java:142)
              at org.jboss.weld.context.AbstractMapContext.destroy(AbstractMapContext.java:159)
              at org.jboss.weld.context.AbstractThreadLocalMapContext.destroy(AbstractThreadLocalMapContext.java:68)
              at org.jboss.weld.conversation.AbstractConversationManager.cleanupConversation(AbstractConversationManager.java:179)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)
              at org.jboss.weld.conversation.ServletConversationManager_$$_javassist_20.cleanupConversation(ServletConversationManager_$$_javassist_20.java)
              at org.jboss.weld.jsf.WeldPhaseListener.afterRenderResponse(WeldPhaseListener.java:132)
              at org.jboss.weld.jsf.WeldPhaseListener.afterPhase(WeldPhaseListener.java:103)
              at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189)
              at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107)
              at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
              at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
              at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
              at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
              at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
              at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
              at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
              at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
              at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
              at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
              at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
              at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
              at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
              at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
              at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
              at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
              at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
              at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
              at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
              at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
              at java.lang.Thread.run(Thread.java:637)
      Caused by: java.lang.IllegalStateException
              at com.sun.enterprise.container.common.impl.EntityManagerWrapper.close(EntityManagerWrapper.java:826)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.jboss.weld.bean.builtin.CallableMethodHandler.invoke(CallableMethodHandler.java:46)
              at javax.persistence.EntityManager_$$_javassist_8.close(EntityManager_$$_javassist_8.java)
              at org.jboss.weld.bean.builtin.ee.PersistenceContextProducerField.defaultDispose(PersistenceContextProducerField.java:58)
              at org.jboss.weld.bean.builtin.ee.PersistenceContextProducerField.defaultDispose(PersistenceContextProducerField.java:29)
              at org.jboss.weld.bean.ProducerField$1.dispose(ProducerField.java:88)
              at org.jboss.weld.bean.ProducerField.destroy(ProducerField.java:113)
              at org.jboss.weld.context.ForwardingContextual.destroy(ForwardingContextual.java:18)
              at org.jboss.weld.context.DependentInstancesStore.destroy(DependentInstancesStore.java:79)
              at org.jboss.weld.context.DependentInstancesStore.destroyDependentInstances(DependentInstancesStore.java:73)
              at org.jboss.weld.context.CreationalContextImpl.release(CreationalContextImpl.java:79)
              at org.glassfish.weld.services.JCDIServiceImpl$JCDIInjectionContextImpl.cleanup(JCDIServiceImpl.java:214)
              at com.sun.ejb.containers.BaseContainer.cleanupInstance(BaseContainer.java:1640)
              at com.sun.ejb.containers.StatefulSessionContainer.forceDestroyBean(StatefulSessionContainer.java:1064)
              at com.sun.ejb.containers.StatefulSessionContainer.removeBean(StatefulSessionContainer.java:1031)
              ... 48 more





        • 2. Re: CDI injected EntityManager into @ConversationScoped SFSB.
          Justin Wyer Newbie

          Thanks Nicklas! The stack traces are the same, however I don't have the problem if I @SessionScoped the SFSB, I only have a problem if its @ConversationScoped.


          I'll build weld off the trunk and see if that solves the problem.

          • 3. Re: CDI injected EntityManager into @ConversationScoped SFSB.
            Justin Wyer Newbie

            Well I built the trunk, but integration with glassfish is dead because the signature for org.jboss.weld.bootstrap.WeldBootstrap.getManager(BeanDeploymentArchive beanDeploymentArchive) has changed because the class org.jboss.weld.BeanManagerImpl has moved to org.jboss.weld.manager.BeanManagerImpl


            Weld users will be unable to drop a new version of weld into ANY container that has integrated against version 1.0.0, is such a massive api change okay for a 1.0.0 to 1.0.1 version bump?




            • 4. Re: CDI injected EntityManager into @ConversationScoped SFSB.
              Pete Muir Master

              BeanManagerImpl is a implementation class, so moving it is not an API change. Can you post the stack trace from GlassFish? I suspect there may have been an integration error.

              • 5. Re: CDI injected EntityManager into @ConversationScoped SFSB.
                Pete Muir Master

                NB WeldBootstrap is also an implementation class.

                • 6. Re: CDI injected EntityManager into @ConversationScoped SFSB.
                  Justin Wyer Newbie

                  Here is the stack trace. I'm going to patch the weld integration on the glassfish side, the only problem I can see is that the return on the getManager is not what glassfish is expecting because of the change in package.


                  Pete do you think there a better way that the glassfish integration could be written so that things like this can be avoided in future?



                  org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: java.lang.NoSuchMethodError: org.jboss.weld.bootstrap.WeldBootstrap.getManager(Lorg/jboss/weld/bootstrap/spi/BeanDeploymentArchive;)Lorg/jboss/weld/BeanManagerImpl;
                          at org.apache.catalina.core.StandardContext.start(StandardContext.java:5159)
                          at com.sun.enterprise.web.WebModule.start(WebModule.java:499)
                          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:928)
                          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:912)
                          at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:694)
                          at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1933)
                          at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1605)
                          at com.sun.enterprise.web.WebApplication.start(WebApplication.java:90)
                          at org.glassfish.internal.data.EngineRef.start(EngineRef.java:126)
                          at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:241)
                          at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:236)
                          at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:339)
                          at com.sun.enterprise.v3.server.ApplicationLoaderService.processApplication(ApplicationLoaderService.java:340)
                          at com.sun.enterprise.v3.server.ApplicationLoaderService.postConstruct(ApplicationLoaderService.java:163)
                          at com.sun.hk2.component.AbstractWombImpl.inject(AbstractWombImpl.java:174)
                          at com.sun.hk2.component.ConstructorWomb$1.run(ConstructorWomb.java:87)
                          at java.security.AccessController.doPrivileged(Native Method)
                          at com.sun.hk2.component.ConstructorWomb.initialize(ConstructorWomb.java:84)
                          at com.sun.hk2.component.AbstractWombImpl.get(AbstractWombImpl.java:77)
                          at com.sun.hk2.component.SingletonInhabitant.get(SingletonInhabitant.java:58)
                          at com.sun.hk2.component.LazyInhabitant.get(LazyInhabitant.java:107)
                          at com.sun.hk2.component.AbstractInhabitantImpl.get(AbstractInhabitantImpl.java:60)
                          at com.sun.enterprise.v3.server.AppServerStartup.run(AppServerStartup.java:236)
                          at com.sun.enterprise.v3.server.AppServerStartup.start(AppServerStartup.java:128)
                          at com.sun.enterprise.module.bootstrap.Main.launch(Main.java:457)
                          at com.sun.enterprise.module.bootstrap.Main.launch(Main.java:401)
                          at org.jvnet.hk2.osgiadapter.HK2Main.start(HK2Main.java:125)
                          at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:640)
                          at org.apache.felix.framework.Felix.activateBundle(Felix.java:1700)
                          at org.apache.felix.framework.Felix.startBundle(Felix.java:1622)
                          at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:915)
                          at org.jvnet.hk2.osgimain.Main.start(Main.java:140)
                          at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:640)
                          at org.apache.felix.framework.Felix.activateBundle(Felix.java:1700)
                          at org.apache.felix.framework.Felix.startBundle(Felix.java:1622)
                          at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1077)
                          at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:264)
                          at java.lang.Thread.run(Thread.java:637)
                  Caused by: java.lang.IllegalArgumentException: javax.servlet.ServletException: java.lang.NoSuchMethodError: org.jboss.weld.bootstrap.WeldBootstrap.getManager(Lorg/jboss/weld/bootstrap/spi/BeanDeploymentArchive;)Lorg/jboss/weld/BeanManagerImpl;
                          at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:2659)
                          at org.apache.catalina.core.StandardContext.addApplicationListener(StandardContext.java:1900)
                          at com.sun.enterprise.web.TomcatDeploymentConfig.configureApplicationListener(TomcatDeploymentConfig.java:277)
                          at com.sun.enterprise.web.TomcatDeploymentConfig.configureWebModule(TomcatDeploymentConfig.java:136)
                          at com.sun.enterprise.web.WebModuleContextConfig.start(WebModuleContextConfig.java:277)
                          at com.sun.enterprise.web.WebModuleContextConfig.lifecycleEvent(WebModuleContextConfig.java:176)
                          at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
                          at org.apache.catalina.core.StandardContext.start(StandardContext.java:5151)
                          ... 37 more
                  Caused by: javax.servlet.ServletException: java.lang.NoSuchMethodError: org.jboss.weld.bootstrap.WeldBootstrap.getManager(Lorg/jboss/weld/bootstrap/spi/BeanDeploymentArchive;)Lorg/jboss/weld/BeanManagerImpl;
                          at org.apache.catalina.core.StandardContext.createListener(StandardContext.java:2769)
                          at org.apache.catalina.core.StandardContext.loadListener(StandardContext.java:4646)
                          at com.sun.enterprise.web.WebModule.loadListener(WebModule.java:1583)
                          at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:2656)
                          ... 44 more
                  Caused by: java.lang.NoSuchMethodError: org.jboss.weld.bootstrap.WeldBootstrap.getManager(Lorg/jboss/weld/bootstrap/spi/BeanDeploymentArchive;)Lorg/jboss/weld/BeanManagerImpl;
                          at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:170)
                          at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:154)
                          at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:456)
                          at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:423)
                          at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:295)
                          at com.sun.enterprise.web.WebContainer.createListenerInstance(WebContainer.java:730)
                          at com.sun.enterprise.web.WebModule.createListenerInstance(WebModule.java:1958)
                          at org.apache.catalina.core.StandardContext.createListener(StandardContext.java:2767)
                          ... 47 more

                  • 7. Re: CDI injected EntityManager into @ConversationScoped SFSB.
                    Nicklas Karlsson Master

                    I think this was a somewhat isolated event - the integration point between an AS and Weld is not that big but unfortunately it hit BeanManagerImpl which (suprise) is quite central.


                    I don't see how this issue can be prevented completely (as integration is going to refer directly to concrete implementation classes and not the stable API) in the future but we could perhaps inform the known integrators in advance if this is planned to happen again in the future.

                    • 8. Re: CDI injected EntityManager into @ConversationScoped SFSB.
                      Justin Wyer Newbie

                      Ok so I rebuilt the glassfish weld integration and got passed the problem above there is one place in the glassfish code where the BeanManagerImpl is called from and this is a bug on their side as far as I can tell: (org.glassfish.weld.WebComponentInjectionManager.java:75) they should be importing and using BeanManager instead.


                      On the weld side I exported org.jboss.weld.manager.api package and the full org.jboss.weld.jsf package via osgi, the glassfish guys did this in their repo.


                      I'll submit a bug and patch to the glassfish guys.


                      Now however, it is time for lunch.

                      • 9. Re: CDI injected EntityManager into @ConversationScoped SFSB.
                        Pete Muir Master

                        Justin Wyer wrote on Jan 14, 2010 11:31:


                        Ok so I rebuilt the glassfish weld integration and got passed the problem above there is one place in the glassfish code where the BeanManagerImpl is called from and this is a bug on their side as far as I can tell: (org.glassfish.weld.WebComponentInjectionManager.java:75) they should be importing and using BeanManager instead.

                        On the weld side I exported org.jboss.weld.manager.api package and the full org.jboss.weld.jsf package via osgi, the glassfish guys did this in their repo.

                        I'll submit a bug and patch to the glassfish guys.

                        Now however, it is time for lunch.


                        Thanks Justin, that is great!


                        Please submit me a patch in JIRA for any OSGi changes, we maintain those in the Weld repo.



                        Nicklas Karlsson


                        I think this was a somewhat isolated event - the integration point between an AS and Weld is not that big but unfortunately it hit BeanManagerImpl which (suprise) is quite central.

                        I don't see how this issue can be prevented completely (as integration is going to refer directly to concrete implementation classes and not the stable API) in the future but we could perhaps inform the known integrators in advance if this is planned to happen again in the future.


                        Nonsense, this is totally avoidable if you follow our integration guidelines! This is precisely why we publish (a) an integration guide, and (b) a SPI for Weld, to clearly demarcate the lines between impl and api :-) In this case, an integration should never refer to BeanMangerImpl, but to WeldManager, or to BeanManager. If there is missing functionality, which is on BeanManagerImpl, the correct thing to do is ask for it to be exposed, not just to ignore good practices.


                        To reiterate, an integration should never refer to non SPI classes, except where explicitly permitted by the integration guidelines (and even this is a bit of a hack and will be fixed when we have time).