1 Reply Latest reply on Oct 21, 2010 9:34 PM by andrewwheeler

    Extended persistence and PostConstruct

    andrewwheeler

      I have a problem with a conversation (or session) scoped stateful session bean invoking a @PostConstruct method:



      Caused by: java.lang.NullPointerException
              at org.jboss.ejb3.entity.ExtendedEntityManager.getPersistenceContext(ExtendedEntityManager.java:76)
              at org.jboss.ejb3.entity.ExtendedEntityManager.getEntityManager(ExtendedEntityManager.java:61)
              at org.jboss.ejb3.jpa.integration.JPA1EntityManagerDelegator.find(JPA1EntityManagerDelegator.java:81)
              at volcanic.tephra.action.person.PersonHome.createInstance(PersonHome.java:58)
              at volcanic.tephra.action.person.PersonHome.createInstance(PersonHome.java:1)
              at volcanic.tephra.action.framework.EntityHome.initialiseInstance(EntityHome.java:40)
              at volcanic.tephra.action.framework.EntityHome.getInstance(EntityHome.java:31)
              at volcanic.tephra.action.person.PersonHome.getInstance(PersonHome.java:68)
              at volcanic.tephra.action.person.PersonHome.postConstruct(PersonHome.java:95)
      



      The post construct code attempts to access the extended persistence context at which point it blows. This does not occur using the standard persistence context, but of course that results in lazy initialisation exceptions. Could it be that the context is not yet established therefore the extended context cannot be determined? I might have to go digging in the JBoss ejb3 code to see what it is trying to do. It really should catch whatever is causing the NPE a little more gracefully.


      The weld documentation states:



      The Java EE @PostConstruct and @PreDestroy callbacks are also supported for all managed beans. The @PostConstruct method is called after all  injection has been performed.

      From the stack trace it does not appear that ALL the injection has been done as the persistence context is lazy loaded when first accessed.


      Running JBossAS-M5 with a WAR project.

        • 1. Re: Extended persistence and PostConstruct
          andrewwheeler

          Well just as I thought. It appears there is no context established before the PostConstruct is invoked. From the code in ExtendedEntityManager the beanContext must be null.



             public EntityManager getPersistenceContext()
             {
                StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
                EntityManager persistenceContext = beanContext.getExtendedPersistenceContext(identity);
                if (persistenceContext == null)
                   throw new RuntimeException("Unable to determine persistenceContext: " + identity
                                              + " in injected SFSB: " + beanContext.getContainer().getObjectName());
                return persistenceContext;
             }
          



          This may have been around for a while, see https://jira.jboss.org/browse/EJBTHREE-912. I guess I'll just work around it for the moment.