Uploaded image for project: 'WildFly'
  1. WildFly
  2. WFLY-2387

CDI injection in entity listeners failing

    Details

      Description

      When trying to use CDI injection in JPA entity listeners, deployment fails with the following exception:

      16:16:37,448 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 15) MSC000001: Failed to start service jboss.persistenceunit."inject-ear.ear#primary": org.jboss.msc.service.StartException in service jboss.persistenceunit."inject-ear.ear#primary": java.lang.IllegalStateException: JBAS016071: Singleton not set for org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader@4eeb95dc. This means that you are trying to access a weld deployment with a Thread Context ClassLoader that is not associated with the deployment.
      	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:169)
      	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117)
      	at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_25]
      	at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:463) [wildfly-security-manager-1.0.0.Beta3.jar:1.0.0.Beta3]
      	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:178)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_25]
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_25]
      	at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
      	at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]
      Caused by: java.lang.IllegalStateException: JBAS016071: Singleton not set for org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader@4eeb95dc. This means that you are trying to access a weld deployment with a Thread Context ClassLoader that is not associated with the deployment.
      	at org.jboss.as.weld.services.ModuleGroupSingletonProvider$TCCLSingleton.get(ModuleGroupSingletonProvider.java:75)
      	at org.jboss.as.weld.services.ModuleGroupSingletonProvider$TCCLSingleton.get(ModuleGroupSingletonProvider.java:128)
      	at org.jboss.weld.Container.instance(Container.java:65)
      	at org.jboss.weld.manager.BeanManagerImpl.getBeans(BeanManagerImpl.java:563)
      	at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:90)
      	at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:358)
      	at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:369)
      	at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:72)
      	at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:60)
      	at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:66)
      	at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
      	at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:64)
      	at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:90)
      	at org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory$BeanMetaData.<init>(BeanManagerListenerFactory.java:82)
      	at org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory$BeanMetaData.<init>(BeanManagerListenerFactory.java:71)
      	at org.hibernate.jpa.event.internal.jpa.BeanManagerListenerFactory.buildListener(BeanManagerListenerFactory.java:57)
      	at org.hibernate.jpa.event.internal.jpa.LegacyCallbackProcessor.resolveCallbacks(LegacyCallbackProcessor.java:168)
      	at org.hibernate.jpa.event.internal.jpa.LegacyCallbackProcessor.processCallbacksForEntity(LegacyCallbackProcessor.java:71)
      	at org.hibernate.jpa.event.spi.JpaIntegrator.integrate(JpaIntegrator.java:150)
      	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310)
      	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1837)
      	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:854)
      	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:847)
      	at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:396)
      	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:846)
      	at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
      	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:151)
      	... 8 more
      

      I've created a small showcase of the problem: https://github.com/papegaaij/listener-injection

        Gliffy Diagrams

          Issue Links

            Activity

            Hide
            mkouba Martin Kouba added a comment -

            Alexander Sparkowsky I see. So your workaround is not used for JPA entity listeners at all. Nevertheless, thanks a lot for a good tip!

            Show
            mkouba Martin Kouba added a comment - Alexander Sparkowsky I see. So your workaround is not used for JPA entity listeners at all. Nevertheless, thanks a lot for a good tip!
            Hide
            smarlow Scott Marlow added a comment -

            I pushed a (work in progress) hack to https://github.com/scottmarlow/wildfly/tree/orm51 that passes the org.jboss.as.test.integration.ee.injection.support.jpa.EntityListenerInjectionSupportTestCase test case that Tomas Remes provided. This is with the https://hibernate.atlassian.net/browse/HHH-8706 changes that Steve Ebersole push to the Hibernate master branch.

            I also tried the https://hibernate.atlassian.net/browse/HHH-10477 change on the ORM 5.0 branch, with the EntityListenerInjectionSupportTestCase test changed to set persistence unit property "hibernate.delay_cdi_access=true" and that also passed.

            Additional status updates are in comments on HHH-8706.

            Show
            smarlow Scott Marlow added a comment - I pushed a (work in progress) hack to https://github.com/scottmarlow/wildfly/tree/orm51 that passes the org.jboss.as.test.integration.ee.injection.support.jpa.EntityListenerInjectionSupportTestCase test case that Tomas Remes provided. This is with the https://hibernate.atlassian.net/browse/HHH-8706 changes that Steve Ebersole push to the Hibernate master branch. I also tried the https://hibernate.atlassian.net/browse/HHH-10477 change on the ORM 5.0 branch, with the EntityListenerInjectionSupportTestCase test changed to set persistence unit property "hibernate.delay_cdi_access=true" and that also passed. Additional status updates are in comments on HHH-8706.
            Hide
            renannp Renann Prado added a comment - - edited

            Hello, guys.

            I'm trying to use @PostPersist instead of using a database trigger after insert because I read somewhere that it would cause issues if I use second level cache, so I pretty much want to do this:

            import javax.persistence.PostPersist;
             
            public class RegularEntityListener {
             
                @EJB
                private EntityHistoryFacade entityHistoryFacade;
             
                @EJB
                private EntityFacade entityFacade;
             
                @PostPersist
                public void postPersist(final Entity regularEntity) {
                    // copy the regularEntity to the entityHistory table
                }
            }
            

            Do you guys have a workaround for this in Wildlfy 10/9?

            Thanks!!

            Show
            renannp Renann Prado added a comment - - edited Hello, guys. I'm trying to use @PostPersist instead of using a database trigger after insert because I read somewhere that it would cause issues if I use second level cache, so I pretty much want to do this: import javax.persistence.PostPersist;   public class RegularEntityListener {   @EJB private EntityHistoryFacade entityHistoryFacade;   @EJB private EntityFacade entityFacade;   @PostPersist public void postPersist( final Entity regularEntity) { // copy the regularEntity to the entityHistory table } } Do you guys have a workaround for this in Wildlfy 10/9? Thanks!!
            Hide
            swd847 Stuart Douglas added a comment -

            AFAIK according to the JPA spec you should not be modifying/adding entities in the PostPersist event listener, so this approach will not work correctly anyway (in my experience it will work most of the time, and occasionally fail in a really hard to diagnose way).

            Show
            swd847 Stuart Douglas added a comment - AFAIK according to the JPA spec you should not be modifying/adding entities in the PostPersist event listener, so this approach will not work correctly anyway (in my experience it will work most of the time, and occasionally fail in a really hard to diagnose way).
            Hide
            renannp Renann Prado added a comment -

            So should I rely on a database trigger? What about the cache?
            I know it's offtopic, but if you could answer, please...

            Show
            renannp Renann Prado added a comment - So should I rely on a database trigger? What about the cache? I know it's offtopic, but if you could answer, please...

              People

              • Assignee:
                smarlow Scott Marlow
                Reporter:
                papegaaij Emond Papegaaij
              • Votes:
                22 Vote for this issue
                Watchers:
                34 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Development