1 2 Previous Next 17 Replies Latest reply on Dec 10, 2011 11:21 AM by smarlow

    EJBContext not bounded to JNDI in EntityListener

    jw

      Were are using EntityListeners to log the name of the user changing the entity. The entity is annotated like

       

       

      @Entity
      @EntityListeners({ MyListener.class })
      public class MyEntity {
      ... 
      }
      
      

       

       

      And the listener

       

       

      public class MyListener {
      
          @PreUpdate
          @PrePersist
          public void onPrePersist(Object entity) {
                EJBContext ctx;
      
                ...
                ctx = createInitialContext().lookup("java:comp/EJBContext");
                System.out.println(ctx.getCallerPrincipal().getName());
      
                ...
           }
      }
      
      

       

       

      That works fine on JBoss4. In AS7 ctx is always null. EJBContext seems not to be bounded. The lookup works in the EJB itself (AS4 and AS7).

      Shouldn't the listener run in the same context as the ejb?

       

      Any other solutions to get principal name in a listener?

       

       


        • 1. Re: EJBContext not bounded to JNDI in EntityListener
          nickarls

          Tried with a @Resource annotation on the field?

          • 2. Re: EJBContext not bounded to JNDI in EntityListener
            jw

            yes, but doesn't work. It's null too.

            • 3. Re: EJBContext not bounded to JNDI in EntityListener
              nickarls

              Shouldn't the field be on class level(?)

              • 4. Re: EJBContext not bounded to JNDI in EntityListener
                jw

                Nicklas Karlsson schrieb:

                 

                Shouldn't the field be on class level(?)

                Yes, I've moved it up for the test with annotation. It wouldn't compile otherwise.

                • 5. Re: EJBContext not bounded to JNDI in EntityListener
                  jaikiran

                  I don't anything in the JPA spec which says entity listeners have injection support. Even if they did you wouldn't be able to inject a EJBContext because an entity listener is not an EJB.

                  • 6. Re: EJBContext not bounded to JNDI in EntityListener
                    sfcoy

                    jw may get away with injecting the Principal using Solder

                    • 7. Re: EJBContext not bounded to JNDI in EntityListener
                      jw

                      jaikiran pai schrieb:

                       

                      I don't anything in the JPA spec which says entity listeners have injection support. Even if they did you wouldn't be able to inject a EJBContext because an entity listener is not an EJB.

                      Using annotation was just a try for a workaround.

                      What I did in Jboss4 was a JNDI lookup to the EJBContext of the enclosing EJB (Session Bean) that

                      is storing the entity bean.

                      • 8. Re: EJBContext not bounded to JNDI in EntityListener
                        smarlow

                        To backup what Jaikiran said, here is some text from the jpa 2.0 specification section 3.5

                         

                        Lifecycle callbacks can invoke JNDI, JDBC, JMS, and enterprise beans.

                        and

                        When invoked from within a Java EE environment, the callback listeners for an entity share the enterprise

                        naming context of the invoking component, and the entity callback methods are invoked in the

                        transaction and security contexts of the calling component at the time at which the callback method is

                        invoked.

                         

                        So, you can use the context to lookup a bean.  Also, as Jaikiran said, there is no EJBContext because an entity listener is not an EJB. 

                        • 9. Re: EJBContext not bounded to JNDI in EntityListener
                          smarlow

                          Are you calling from an EJB bean?  Can you give an example of the bean code that is using the entity manager that causes the entity listener to be invoked.

                          • 10. Re: EJBContext not bounded to JNDI in EntityListener
                            jw

                            Scott Marlow schrieb:

                             

                            Are you calling from an EJB bean?  Can you give an example of the bean code that is using the entity manager that causes the entity listener to be invoked.

                            Yes.

                             

                            Here the code snippet

                             

                            @Stateless
                            @Interceptors(ExceptionLogInterceptor.class)
                            @PersistenceContext(name = "persistence/MyEM", unitName = "MyPU")
                            public class MyServiceBean implements MyServiceRemote {
                            
                                @Override
                                public MyEntity store(MyEntity entity) {
                                    return MyDao.store(entity); // gets entity manager via jndi lookup
                                }
                            }
                            

                             

                             

                            While prepareing the test I've noticed the following:

                             

                            In @PrePersist the EJBContext is available, but in @PreUpdate not

                            • 11. Re: EJBContext not bounded to JNDI in EntityListener
                              smarlow

                              Interesting, could you add a call to Thread.dumpStack() from both the @PrePersist and the @PreUpdate calls?

                               

                              Please share the output here. 

                               

                              Thanks for reporting this.

                              • 12. Re: EJBContext not bounded to JNDI in EntityListener
                                jw

                                sure, will do it as soon as possible (friday)...

                                • 13. Re: EJBContext not bounded to JNDI in EntityListener
                                  smarlow

                                  Great, I think that will help us better understand the problem.

                                  • 14. Re: EJBContext not bounded to JNDI in EntityListener
                                    jw

                                    Here the dumps:

                                     

                                    @PreCreate:

                                          at java.lang.Thread.dumpStack(Thread.java:1249)
                                          at MyListener.onPrePersist(AuditorListener.java:48)
                                          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.hibernate.ejb.event.ListenerCallback.invoke(ListenerCallback.java:48)
                                          at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:110)
                                          at org.hibernate.ejb.event.EntityCallbackHandler.preCreate(EntityCallbackHandler.java:79)
                                          at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:77)
                                          at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:180)
                                          at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:136)
                                          at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:64)
                                          at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:727)
                                          at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:703)
                                          at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:707)
                                          at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:847)
                                          at org.jboss.as.jpa.container.AbstractEntityManager.persist(AbstractEntityManager.java:574)
                                          at MyDao.persist(AnyDao.java:128)
                                          at MyDao.store(AnyDao.java:215)
                                          at MyServiceBean.store(ScheduleDataServiceBean.java:53)
                                          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.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)
                                          at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.delegateInterception(Jsr299BindingsInterceptor.java:109)
                                          at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:120)
                                          at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:130)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)
                                          at ExceptionLogInterceptor.intercept(ExceptionLogInterceptor.java:36)
                                          at sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source)
                                          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                                          at java.lang.reflect.Method.invoke(Method.java:597)
                                          at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptorFactory$ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptorFactory.java:123)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:45)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
                                          at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:307)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:201)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropogatingInterceptor.processInvocation(EJBRemoteTransactionPropogatingInterceptor.java:86)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:44)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:55)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
                                          at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)
                                          at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.invokeMethod(MethodInvocationMessageHandler.java:256)
                                          at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.access$200(MethodInvocationMessageHandler.java:56)
                                          at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler$1.run(MethodInvocationMessageHandler.java:177)
                                          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
                                          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                                          at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                                          at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                                          at java.lang.Thread.run(Thread.java:662)
                                          at org.jboss.threads.JBossThread.run(JBossThread.java:122)
                                    

                                     

                                    @PreUpdate:

                                          at java.lang.Thread.dumpStack(Thread.java:1249)
                                          at MyListener.onPreUpdate(AuditorListener.java:57)
                                          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.hibernate.ejb.event.ListenerCallback.invoke(ListenerCallback.java:48)
                                          at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:110)
                                          at org.hibernate.ejb.event.EntityCallbackHandler.preUpdate(EntityCallbackHandler.java:95)
                                          at org.hibernate.ejb.event.EJB3FlushEntityEventListener.invokeInterceptor(EJB3FlushEntityEventListener.java:65)
                                          at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:315)
                                          at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:266)
                                          at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:149)
                                          at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235)
                                          at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
                                          at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
                                          at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1082)
                                          at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:317)
                                          at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:104)
                                          at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53)
                                          at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:97)
                                          at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:274)
                                          at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:94)
                                          at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:159)
                                          at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1159)
                                          at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:119)
                                          at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:92)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:234)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:307)
                                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:201)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropogatingInterceptor.processInvocation(EJBRemoteTransactionPropogatingInterceptor.java:86)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:44)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:55)
                                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
                                          at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
                                          at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)
                                          at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.invokeMethod(MethodInvocationMessageHandler.java:256)
                                          at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.access$200(MethodInvocationMessageHandler.java:56)
                                          at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler$1.run(MethodInvocationMessageHandler.java:177)
                                          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
                                          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                                          at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                                          at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                                          at java.lang.Thread.run(Thread.java:662)
                                          at org.jboss.threads.JBossThread.run(JBossThread.java:122)
                                    

                                     

                                     

                                     

                                    When I change the session bean's transaction control from Container to Bean Managed and starting a User Transaction, the context is in both cases available. So the problem occures only when you use container managed transactions.

                                    1 2 Previous Next