0 Replies Latest reply on Apr 4, 2012 8:29 AM by Carlos Eugênio Purificação

    EntityManager injection on ServletContextListener not working with @Inject only @PersistenceContext

    Carlos Eugênio Purificação Newbie

      Hi,

       

         I've created a simple test to show a problem I am having when trying to inject EntityManager objects on a ServletContextListener using CDI @Inject on JBoss 6.1 Final (or JBoss 6.0 Final):

       

      @WebListener
      public class ContextListenerTest implements ServletContextListener { 
                @Inject
                Logger log;
      
                @Inject
                private EntityManager manager;
      
                @PersistenceContext
                private EntityManager ctxManager;
      
                @Override
                public void contextInitialized(ServletContextEvent sce) {
                          log.info("Context Initialized!");
                          log.info("Manager: " + manager); // This is null on JBoss 6.1 using JTA
                          log.info("ContextManager: " + ctxManager); // This is ok on JBoss 6.1 using JTA.
                }
      
                  @Override
                public void contextDestroyed(ServletContextEvent sce) {
                }
      }
      

       

      Here is the output:

       

      08:46:22,794 INFO  [STDOUT] 394  INFO  ContextListenerTest - Context Initialized!
      08:46:22,795 INFO  [STDOUT] 395  INFO  ContextListenerTest - Manager: null
      08:46:22,795 INFO  [STDOUT] 395  INFO  ContextListenerTest - ContextManager: org.jboss.jpa.tx.TransactionScopedEntityManager@103f52a
      
      
      

       

      As the output shows using @Inject doesn't work on this context.

       

      And if we do something like this:

       

      public interface MyInterface {}
      
      public class MyInterfaceImpl implements MyInterface { 
                @Inject
                EntityManager manager; //Using @PersistenceContext would be ok here.
      }
      
      @WebListener
      public class ContextListenerTest2 implements ServletContextListener { 
      
                @Inject
                Logger log;
      
                @Inject
                private Instance<MyInterface> instances;
      
                @Override
                public void contextInitialized(ServletContextEvent sce) {
                          log.info("Context Initialized2!");
                          Iterator<MyInterface> it = instances.iterator();
                          log.info("Instances iterator: " + it + ", unSatisfied: " + instances.isUnsatisfied());
                          while (it.hasNext()) {
                                    MyInterface impl = it.next();
                                    log.info("Implementation: " + impl);
                          }
                }
      
                @Override
                public void contextDestroyed(ServletContextEvent sce) {
                }
      }
      
      

       

      The results are even worse:

       

      09:15:18,211 INFO  [STDOUT] 336  INFO  view.ContextListenerTest2 - Context Initialized2!
      09:15:18,211 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/projecttest]] Exception sending context initialized event to listener instance of class 
      view.ContextListenerTest2: org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped
                at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:667) [:6.1.0.Final]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:693) [:6.1.0.Final]
                at org.jboss.weld.bean.AbstractReceiverBean.getReceiver(AbstractReceiverBean.java:84) [:6.1.0.Final]
                at org.jboss.weld.bean.ProducerMethod$1.produce(ProducerMethod.java:146) [:6.1.0.Final]
                at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:361) [:6.1.0.Final]
                at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:67) [:6.1.0.Final]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:693) [:6.1.0.Final]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:775) [:6.1.0.Final]
                at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:138) [:6.1.0.Final]
                at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:875) [:6.1.0.Final]
                at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:887) [:6.1.0.Final]
                at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1$1.proceed(ManagedBean.java:182) [:6.1.0.Final]
                at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:54) [:6.1.0.Final]
                at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1.work(ManagedBean.java:176) [:6.1.0.Final]
                at org.jboss.weld.bean.ManagedBean$FixInjectionPoint.run(ManagedBean.java:142) [:6.1.0.Final]
                at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.inject(ManagedBean.java:170) [:6.1.0.Final]
                at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:339) [:6.1.0.Final]
                at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:67) [:6.1.0.Final]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:693) [:6.1.0.Final]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:731) [:6.1.0.Final]
                at org.jboss.weld.bean.builtin.InstanceImpl.iterator(InstanceImpl.java:147) [:6.1.0.Final]
                at view.ContextListenerTest2.contextInitialized(ContextListenerTest2.java:25) [:]
                at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3369) [:6.1.0.Final]
                at org.apache.catalina.core.StandardContext.start(StandardContext.java:3828) [:6.1.0.Final]
                at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:294) [:6.1.0.Final]
                at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:146) [:6.1.0.Final]
                at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:476) [:6.1.0.Final]
                at org.jboss.web.deployers.WebModule.startModule(WebModule.java:118) [:6.1.0.Final]
                at org.jboss.web.deployers.WebModule.start(WebModule.java:95) [:6.1.0.Final]
                at sun.reflect.GeneratedMethodAccessor620.invoke(Unknown Source) [:1.6.0_23]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_23]
                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_23]
                at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157) [:6.0.0.GA]
                at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96) [:6.0.0.GA]
                at org.jboss.mx.server.Invocation.invoke(Invocation.java:88) [:6.0.0.GA]
                at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:271) [:6.0.0.GA]
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:670) [:6.0.0.GA]
                at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:206) [:2.2.0.SP2]
                at $Proxy41.start(Unknown Source)          at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:53) [:2.2.0.SP2]
                at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:41) [:2.2.0.SP2]
                at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.system.microcontainer.ServiceControllerContext.install(ServiceControllerContext.java:301) [:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:939) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:654) [jboss-dependency.jar:2.2.0.SP2]
                at org.jboss.deployers.plugins.deployers.DeployersImpl.change(DeployersImpl.java:1983) [:2.2.2.GA]
                at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:1076) [:2.2.2.GA]
                at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:679) [:2.2.2.GA]
                at org.jboss.system.server.profileservice.deployers.MainDeployerPlugin.process(MainDeployerPlugin.java:106) [:6.1.0.Final]
                at org.jboss.profileservice.dependency.ProfileControllerContext$DelegateDeployer.process(ProfileControllerContext.java:143) [:0.2.2]
                at org.jboss.profileservice.deployment.hotdeploy.HDScanner$HDScanAction.deploy(HDScanner.java:240) [:0.2.2]
                at org.jboss.profileservice.deployment.hotdeploy.HDScanner$HDScanAction.complete(HDScanner.java:192) [:0.2.2]
                at org.jboss.profileservice.management.TwoPCActionWrapper.doComplete(TwoPCActionWrapper.java:57) [:0.2.2]
                at org.jboss.profileservice.management.actions.AbstractTwoPhaseModificationAction.complete(AbstractTwoPhaseModificationAction.java:74) [:0.2.2]
                at org.jboss.profileservice.management.actions.AbstractTwoPhaseModificationAction.prepare(AbstractTwoPhaseModificationAction.java:95) [:0.2.2]
                at org.jboss.profileservice.management.ModificationSession.prepare(ModificationSession.java:87) [:0.2.2]
                at org.jboss.profileservice.management.AbstractActionController.internalPerfom(AbstractActionController.java:234) [:0.2.2]
                at org.jboss.profileservice.management.AbstractActionController.performWrite(AbstractActionController.java:213) [:0.2.2]
                at org.jboss.profileservice.management.AbstractActionController.perform(AbstractActionController.java:150) [:0.2.2]
                at org.jboss.profileservice.management.AbstractActionController.perform(AbstractActionController.java:135) [:0.2.2]
                at org.jboss.profileservice.deployment.hotdeploy.HDScanner.scan(HDScanner.java:146) [:0.2.2]
                at org.jboss.profileservice.deployment.hotdeploy.HDScanner.run(HDScanner.java:90) [:0.2.2]
                at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) [:1.6.0_23]
                at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) [:1.6.0_23]
                at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) [:1.6.0_23]
                at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) [:1.6.0_23]
                at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180) [:1.6.0_23]
                at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204) [:1.6.0_23]
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_23]
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_23]
                at java.lang.Thread.run(Thread.java:662) [:1.6.0_23]
      
      

       

      Can anyone confirm if this is a bug or is there any configuration issues?

       

      Follows the relevant part of persistence.xml

       

      <persistence-unit name="bookmark-ds" transaction-type="RESOURCE_LOCAL">
       <non-jta-data-source>java:/DefaultDS</non-jta-data-source> 
       <class>domain.Bookmark</class>
       <properties>
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.format_sql" value="false" />
                <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
       </properties>
      </persistence-unit>
      
      

       

      Thanks in advance,

      Carlos Eugênio.