2 Replies Latest reply on Jul 7, 2006 12:46 PM by lucluc

    Injecting EntityManager with an interceptor

    sbalmos

      Solve one question, end up finding another. ;)

      I'm trying to get my EntityManager injected into my DAO layer, by specifying the core class of the DAO as an interceptor to my SLSB, which is acting as a SOAP service endpoint (enough buzzwords?).

      I know by the deployment logs that the interceptor is being registered correctly. But no matter what, the EntityManager never gets injected, and remains null.

      The persistence unit is configured in a separate persistence JAR from the EJB JAR, and everything is packed into an EAR. See below for the relevant annotations and snippets from everything:

      @Stateless @WebService
      @Interceptors({EJB3DAOFactory.class})
      public class SOAPBackend
      {
      ...
      }

      public class EJB3DAOFactory
      {
      private static final Logger log = Logger.getLogger(EJB3DAOFactory.class);

      @PersistenceContext(unitName = "simunex")
      protected EntityManager em;

      public EJB3DAOFactory() {}

      protected EntityManager getEntityManager()
      {
      if (em == null) { log.debug("EntityManager is null!"); }
      return em;
      }
      }

      Like I said, the deployment logs correctly register the interceptor-based dependency on the EntityManager, and defers starting of the EJB until after Hibernate starts up. But whenever I attempt to perform a SOAP operation, which is serviced by SOAPBackend, the EntityManager remains null.

      Options? I'd rather stay away from global JNDI lookup if possible. I know about the scoped deployment of the persistence unit. But I would've thought that a persistence unit defined in one JAR of an EAR would be within the scope of another JAR of the EAR.

      Thanks!

      --Scott

        • 1. Re: Injecting EntityManager with an interceptor
          sbalmos

          Yet again answering myself...

          See http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3953596

          But seriously, like the original poster in that thread, why must I create a separate remote endpoint interface for the interceptor to be called? It shouldn't be necessary.

          --Scott

          • 2. Re: Injecting EntityManager with an interceptor
            lucluc

            Are you sure you are interested in having the EntityManager created the first time the factory (singleton) is created? And then using that EM instance? I don't think EM is thread safe.

            I had the same decision to take some time ago, and I preferred to pass EM as parameter to DAO class when calling static method. Doing so, I'm sure to pass the EM private to current thread.

            More: I create a bunch of EM in my interceptors and I store them in threadlocal variables so to get visibility both in other interceptors and bean.
            Every piece of code gets what it needs and passes it to DAO.
            It's useful to put jboss classes on debug level in log4j conf. Doing that you could have a look at EM lifecycle.

            Tell me what you think.
            bye
            F