Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException during eager initialization in CDI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException during eager initialization in CDI

    Hello,

    During the startup of our application the following happens:

    [12.05.14 09:17:14:244 CEST] 00000015 SystemOut O 2014-05-12 09:17:14,244 DEBUG [ORB.thread.pool : 0] org.springframework.data.repository.cdi.CdiReposit oryExtensionSupport - Eagerly instantiating
    CDI repository bean for interface [DAO interface].
    [12.05.14 09:17:14:244 CEST] 00000015 SystemOut O 2014-05-12 09:17:14,244 DEBUG [ORB.thread.pool : 0] org.springframework.data.repository.cdi.CdiReposit oryBean - Creating CDI repository bean insta
    nce for [DAO interface].
    [12.05.14 09:17:14:353 CEST] 00000015 BeansDeployer E BeansDeployer deploy
    java.lang.NullPointerException
    at com.ibm.ws.jpa.management.JPATxEntityManager.getEM InvocationInfo(JPATxEntityManager.java:201)
    at com.ibm.ws.jpa.management.JPATxEntityManager.getEM InvocationInfo(JPATxEntityManager.java:174)
    at com.ibm.ws.jpa.management.JPAEntityManager.getMeta model(JPAEntityManager.java:536)
    at org.springframework.data.jpa.repository.support.Jp aEntityInformationSupport.getMetadata(JpaEntityInf ormationSupport.java:60)
    at org.springframework.data.jpa.repository.support.Jp aRepositoryFactory.getEntityInformation(JpaReposit oryFactory.java:146)
    at org.springframework.data.jpa.repository.support.Jp aRepositoryFactory.getTargetRepository(JpaReposito ryFactory.java:84)
    at org.springframework.data.jpa.repository.support.Jp aRepositoryFactory.getTargetRepository(JpaReposito ryFactory.java:67)
    at org.springframework.data.repository.core.support.R epositoryFactorySupport.getRepository(RepositoryFa ctorySupport.java:150)
    at org.springframework.data.repository.core.support.R epositoryFactorySupport.getRepository(RepositoryFa ctorySupport.java:129)
    at org.springframework.data.jpa.repository.cdi.JpaRep ositoryBean.create(JpaRepositoryBean.java:69)
    at org.springframework.data.repository.cdi.CdiReposit oryBean.create(CdiRepositoryBean.java:147)
    at org.springframework.data.repository.cdi.CdiReposit oryBean.initialize(CdiRepositoryBean.java:132)
    at org.springframework.data.repository.cdi.CdiReposit oryExtensionSupport.afterDeploymentValidation(CdiR epositoryExtensionSupport.java:134)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.apache.webbeans.event.ObserverMethodImpl.notif y(ObserverMethodImpl.java:285)
    at org.apache.webbeans.event.NotificationManager.fire Event(NotificationManager.java:455)
    at org.apache.webbeans.container.BeanManagerImpl.fire Event(BeanManagerImpl.java:415)
    at org.apache.webbeans.config.BeansDeployer.fireAfter DeploymentValidationEvent(BeansDeployer.java:280)
    at org.apache.webbeans.config.BeansDeployer.deploy(Be ansDeployer.java:186)
    at org.apache.webbeans.lifecycle.AbstractLifeCycle.st artApplication(AbstractLifeCycle.java:124)
    at com.ibm.ws.webbeans.common.CommonLifeCycle.startAp plication(CommonLifeCycle.java:106)
    at com.ibm.ws.webbeans.services.JCDIComponentImpl.sta rtEJBLifeCycle(JCDIComponentImpl.java:571)
    at com.ibm.ws.webbeans.services.JCDIComponentImpl.sta teChanged(JCDIComponentImpl.java:545)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st ateChanged(ApplicationMgrImpl.java:1107)
    at com.ibm.ws.runtime.component.DeployedApplicationIm pl.fireDeployedObjectEvent(DeployedApplicationImpl .java:1352)
    at com.ibm.ws.runtime.component.DeployedApplicationIm pl.setState(DeployedApplicationImpl.java:293)
    at com.ibm.ws.runtime.component.DeployedApplicationIm pl.setState(DeployedApplicationImpl.java:288)
    at com.ibm.ws.runtime.component.DeployedApplicationIm pl.start(DeployedApplicationImpl.java:977)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st artApplication(ApplicationMgrImpl.java:768)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st artApplicationDynamically(ApplicationMgrImpl.java: 1356)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st art(ApplicationMgrImpl.java:2152)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImp l.start(CompositionUnitMgrImpl.java:445)
    at com.ibm.ws.runtime.component.CompositionUnitImpl.s tart(CompositionUnitImpl.java:123)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImp l.start(CompositionUnitMgrImpl.java:388)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImp l.access$500(CompositionUnitMgrImpl.java:116)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImp l$1.run(CompositionUnitMgrImpl.java:663)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAs( ContextManagerImpl.java:5459)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAsS ystem(ContextManagerImpl.java:5585)
    at com.ibm.ws.security.core.SecurityContext.runAsSyst em(SecurityContext.java:255)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImp l.startCompositionUnit(CompositionUnitMgrImpl.java :677)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImp l.startCompositionUnit(CompositionUnitMgrImpl.java :621)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st artApplication(ApplicationMgrImpl.java:1248)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java :49)
    at sun.reflect.GeneratedMethodAccessor15.invoke(Unkno wn Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java :256)
    at javax.management.modelmbean.RequiredModelMBean$4.r un(RequiredModelMBean.java:1148)
    at java.security.AccessController.doPrivileged(Access Controller.java:252)
    at com.ibm.oti.security.CheckedAccessControlContext.s ecurityCheck(CheckedAccessControlContext.java:30)
    at sun.misc.JavaSecurityAccessWrapper.doIntersectionP rivilege(JavaSecurityAccessWrapper.java:41)
    at javax.management.modelmbean.RequiredModelMBean.inv okeMethod(RequiredModelMBean.java:1142)
    at javax.management.modelmbean.RequiredModelMBean.inv oke(RequiredModelMBean.java:995)
    at com.sun.jmx.interceptor.DefaultMBeanServerIntercep tor.invoke(DefaultMBeanServerInterceptor.java:848)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxM BeanServer.java:774)
    at com.ibm.ws.management.AdminServiceImpl$1.run(Admin ServiceImpl.java:1335)
    at com.ibm.ws.security.util.AccessController.doPrivil eged(AccessController.java:118)
    at com.ibm.ws.management.AdminServiceImpl.invoke(Admi nServiceImpl.java:1228)
    at com.ibm.ws.management.application.sync.StartDeploy mentTask.startDeployment(StartDeploymentTask.java: 248)
    at com.ibm.ws.management.application.sync.StartDeploy mentTask.fullAppUpdate(StartDeploymentTask.java:12 0)
    at com.ibm.ws.management.application.sync.StartDeploy mentTask.performTask(StartDeploymentTask.java:108)
    at com.ibm.ws.management.application.sync.AppBinaryPr ocessor$ExpandApp.expand(AppBinaryProcessor.java:1 709)
    at com.ibm.ws.management.application.sync.AppBinaryPr ocessor.postProcessSynchronousExt(AppBinaryProcess or.java:749)
    at com.ibm.ws.management.bla.sync.BLABinaryProcessor. postProcess(BLABinaryProcessor.java:590)
    at com.ibm.ws.management.bla.sync.BLABinaryProcessor. onChangeCompletion(BLABinaryProcessor.java:467)
    at com.ibm.ws.management.bla.sync.BinaryProcessorWrap per.onChangeCompletion(BinaryProcessorWrapper.java :109)
    at com.ibm.ws.management.repository.FileRepository.po stNotify(FileRepository.java:1905)
    at com.ibm.ws.management.repository.FileRepository.up date(FileRepository.java:1414)
    at com.ibm.ws.management.repository.client.LocalConfi gRepositoryClient.update(LocalConfigRepositoryClie nt.java:189)
    at com.ibm.ws.sm.workspace.impl.WorkSpaceMasterReposi toryAdapter.update(WorkSpaceMasterRepositoryAdapte r.java:665)
    at com.ibm.ws.sm.workspace.impl.RepositoryContextImpl .update(RepositoryContextImpl.java:1998)
    at com.ibm.ws.sm.workspace.impl.RepositoryContextImpl .synch(RepositoryContextImpl.java:1946)
    at com.ibm.ws.sm.workspace.impl.WorkSpaceImpl.synch(W orkSpaceImpl.java:549)
    at com.ibm.ws.management.configservice.ConfigServiceI mpl.save(ConfigServiceImpl.java:719)
    [truncated because of message size limit]

    This is on a WebSphere 8.0.0.6 server using EclipseLink 2.5.0. The entities work in unit tests using H2. The DAOs are injected into EJBs (for planned CMT) using CDI which I understand is a mixture that's not recommended? All DAOs carry the @Eager annotation and are derived from CrudRepository (because JpaRepository caused duplicate method errors).

    The DAO in the stacktrace varies, depending on which gets initialized first. It's however always the first eager instantiation after validating the injection points.

  • #2
    Okay, the exception seems to have been caused by getting an entity manager via @PersistenceContext in an EJB and declaring that as a producer. Producing a new entity manager via a factory allowed the eager instantiation to work.

    Now we got the issue that declaring a DAO as a JpaRepository throws the following exception, this doesn't happen if it's declared as a CrudRepository.

    [JSF classes above here]
    Caused by: javax.ejb.EJBException: Injection failure; nested exception is: java.lang.RuntimeException: duplicate method: findAll in [DAO class]_$$_javassist_57
    Caused by: java.lang.RuntimeException: duplicate method: findAll in [DAO class]_$$_javassist_57
    at javassist.util.proxy.ProxyFactory.createClass3(Pro xyFactory.java:509)
    at javassist.util.proxy.ProxyFactory.createClass2(Pro xyFactory.java:486)
    at javassist.util.proxy.ProxyFactory.createClass1(Pro xyFactory.java:422)
    at javassist.util.proxy.ProxyFactory.createClass(Prox yFactory.java:394)
    at org.apache.webbeans.util.SecurityUtil$PrivilegedAc tionForProxyFactory.run(SecurityUtil.java:301)
    at java.security.AccessController.doPrivileged(Access Controller.java:229)
    at org.apache.webbeans.util.SecurityUtil.doPrivileged CreateClass(SecurityUtil.java:184)
    at org.apache.webbeans.proxy.JavassistProxyFactory.ge tProxyClass(JavassistProxyFactory.java:431)
    at org.apache.webbeans.proxy.JavassistProxyFactory.cr eateNormalScopedBeanProxy(JavassistProxyFactory.ja va:215)
    at org.apache.webbeans.container.BeanManagerImpl.getR eference(BeanManagerImpl.java:870)
    at org.apache.webbeans.container.BeanManagerImpl.getI njectableReference(BeanManagerImpl.java:777)
    at com.ibm.ws.webbeans.services.IBMBeanManagerImpl.ge tInjectableReference(IBMBeanManagerImpl.java:186)
    at com.ibm.ws.webbeans.services.InjectInjectionObject Factory.getInjectableReference(InjectInjectionObje ctFactory.java:316)
    at com.ibm.ws.webbeans.services.InjectInjectionObject Factory.getObjectInstance(InjectInjectionObjectFac tory.java:150)
    at com.ibm.ws.webbeans.services.InjectInjectionBindin g.getInjectionObject(InjectInjectionBinding.java:1 01)
    at com.ibm.wsspi.injectionengine.InjectionBinding.get InjectableObject(InjectionBinding.java:1013)
    at com.ibm.wsspi.injectionengine.InjectionTarget.inje ct(InjectionTarget.java:198)
    at com.ibm.ws.injectionengine.AbstractInjectionEngine .inject(AbstractInjectionEngine.java:947)
    at com.ibm.ejs.container.StatelessBeanO.initialize(St atelessBeanO.java:300)
    at com.ibm.ejs.container.BeanOFactory.create(BeanOFac tory.java:147)
    at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome. java:1240)
    at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome. java:1358)
    at com.ibm.ejs.container.activator.UncachedActivation Strategy.atActivate(UncachedActivationStrategy.jav a:88)
    at com.ibm.ejs.container.activator.Activator.preInvok eActivateBean(Activator.java:614)
    at com.ibm.ejs.container.EJSContainer.preInvokeActiva te(EJSContainer.java:4110)
    at com.ibm.ejs.container.EJSContainer.EjbPreInvoke(EJ SContainer.java:3476)
    [..]
    Caused by: javassist.bytecode.DuplicateMemberException: duplicate method: findAll in [DAO class]_$$_javassist_57
    at javassist.bytecode.ClassFile.testExistingMethod(Cl assFile.java:637)
    at javassist.bytecode.ClassFile.addMethod(ClassFile.j ava:613)
    at javassist.util.proxy.ProxyFactory.override(ProxyFa ctory.java:941)
    at javassist.util.proxy.ProxyFactory.overrideMethods( ProxyFactory.java:913)
    at javassist.util.proxy.ProxyFactory.make(ProxyFactor y.java:714)
    at javassist.util.proxy.ProxyFactory.createClass3(Pro xyFactory.java:497)


    I do see this message twice in the logs during startup:
    [14.05.14 13:15:08:813 CEST] 0000001b SystemOut O 2014-05-14 13:15:08,813 INFO [Default : 4] org.springframework.data.jpa.repository.cdi.JpaRep ositoryExtension - Activating CDI extension for Spri
    ng Data JPA repositories.
    [14.05.14 13:15:08:828 CEST] 0000001b SystemOut O 2014-05-14 13:15:08,828 INFO [Default : 4] org.springframework.data.jpa.repository.cdi.JpaRep ositoryExtension - Activating CDI extension for Spri
    ng Data JPA repositories.

    What could cause that?


    EDIT:
    Additionally, using the entity manager factory to make a new EM during the @Produces method allows the eager instantiation to work out but apparently prevents the DAOs from following container managed transactions. So I'd want them to use a new manager during startup and the managed entity manager during any other call, how should I best solve this? This is on Java 6 so I can't simply tell the factory to give me a synchronized entity manager.
    Last edited by DavidBecker; May 14th, 2014, 08:24 AM.

    Comment


    • #3
      My current thought is that I should switch the entity manager returned by my producer after the springdata/openwebbeans start is complete. Are there any hooks I can use to detect that?

      EDIT: Is that even useful or do the repos stick to the EMs they get during the eager instantiation?
      Last edited by DavidBecker; May 15th, 2014, 05:28 AM.

      Comment


      • #4
        My configuration and entity manager provider currently looks like this:

        Code:
        import javax.ejb.Singleton;
        import javax.enterprise.inject.Produces;
        import javax.persistence.EntityManager;
        import javax.persistence.EntityManagerFactory;
        import javax.persistence.PersistenceContext;
        import javax.persistence.PersistenceUnit;
        
        import org.springframework.context.annotation.Configuration;
        import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
        
        @Configuration
        @EnableJpaRepositories(basePackages = { "XXX" })
        @Singleton
        public class EntityManagerProvider {
        
            @PersistenceContext(unitName = "XXX")
            private EntityManager entityManager;
        
            @PersistenceUnit(unitName = "XXX")
            private EntityManagerFactory emf;
        
            @Produces
            public EntityManager entityManager() {
                return this.emf.createEntityManager();
            }
        
            @Produces
            public EntityManagerFactory getEmf() {
                return this.emf;
            }
        
        }
        I create an entity and then call save on the repository. Nothing lands in the database.

        If entityManager() returns the entityManager member value instead of creating a new one then the eager instantiation fails.

        If I manually grab an injected EM and persist my entity with that, then flush I get told that it's not part of the current transaction.

        If I declare a repository as a JpaRepository (currently they're all CrudRepositorys) then I get a duplicate method error when trying to use that repository.

        I can't even think of what an EM provider for a CDI + SpringData application has to look like. Does anyone have a working example for this combination?

        Comment


        • #5
          Our solution to the entitymanager problem was to make the provider give out a proxy for the entity manager that acts as the CMT EM when there's a transaction and an EMF-made EM otherwise. Springdata could probably handle the Websphere a bit better than that...

          Comment

          Working...
          X