7 Replies Latest reply on Feb 20, 2008 9:42 AM by Karen Greene

    EntityManager is null

    Karen Greene Newbie

      Hi,

      I'm trying to create a class that does database retrieval using entity manager. When I call getVpInfo, I notice that the entity manager is null. I need to access this dao from another class in my app. I am not accessing the dao from the client-side code.

      Does anyone know why the entity manager is null. I have also included my persistence.xml

      =======

      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;

      @Stateless
      public class VpInfoDAOBean implements VpInfoDAORemote, VpInfoDAOLocal{

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

      private static VpInfoDAOBean dao = new VpInfoDAOBean();

      public static VpInfoDAOBean getInstance() {return dao;}

      public VpInfo getVpInfo(String appGroup)
      {
      if (em == null)
      System.out.println("Vp em is null");
      return (VpInfo)em.createQuery(
      "from VpInfo v where v.appGroup = :appGroup").setParameter("appGroup", appGroup).getSingleResult();

      }




      }

      ================================
      persistence.xml


      <persistence-unit name="VealMonitorDB">
      <jta-data-source>java:/DefaultDS</jta-data-source>

      <!--

      -->
      <!-- -->

      </persistence-unit>
      <persistence-unit name="VPInfoDB">
      <jta-data-source>java:/DefaultDS</jta-data-source>

      <!--

      -->
      <!-- -->

      </persistence-unit>

        • 1. Re: EntityManager is null
          Andrew Rubinger Master

          How is the calling class accessing an instance of VpInfoDAOBean?

          S,
          ALR

          • 2. Re: EntityManager is null
            Karen Greene Newbie

            Currently, I am using:
            VPInfoDAOImpl vpdao = VPInfoDAOImpl.getInstance();

            I also tried:

            InitialContext ctx = new InitialContext();
            VPInfoDAOLocal dao = (VPInfoDAOLocal) ctx.lookup("VPInfoDB/VpInfoDAOBean/local");

            I tried VPInfoDAORemote also. That did not work either.

            • 3. Re: EntityManager is null
              Andrew Rubinger Master

               

              VPInfoDAOImpl vpdao = VPInfoDAOImpl.getInstance();

              This won't work; here you are constructing a plain object, not accessing an EJB instance from the container.

              InitialContext ctx = new InitialContext();
              VPInfoDAOLocal dao = (VPInfoDAOLocal) ctx.lookup("VPInfoDB/VpInfoDAOBean/local");


              What's not working here? Are you able to perform the lookup, cast, and invoke?

              S,
              ALR

              • 4. Re: EntityManager is null
                Karen Greene Newbie

                When I tried the below, the dao object is null and I cannot call methods on it.

                InitialContext ctx = new InitialContext();
                VPInfoDAOLocal dao = (VPInfoDAOLocal) ctx.lookup("VPInfoDB/VpInfoDAOBean/local");

                • 5. Re: EntityManager is null
                  Karen Greene Newbie

                  Ok, I am very lost. Forgive me, I am new to jboss and ejbs. I've been looking for a solution for several hours and still have not found the answer. I misspoke about the actual issue. The dao object is not null. I get NameNotFoundException because it can't find it. When I bring up my jndi console it's not there, which is why I'm assuming it can't find it.

                  I'm calling the lookup from a message-driven bean. I didn't think this is considered client code and didn't think lookup would even work.

                  If I move the entity manager to the message-driven bean, then it works. However, I get an additional issue in that flush does not commit the updates to the database. I thought perhaps moving the entity manager to a class outside the message-driven bean would solve this issue; however, I have not gotten this to work.

                  • 6. Re: EntityManager is null
                    Andrew Rubinger Master

                    Please paste the code that works, the code you'd like to work, and the relevant part of the JNDIView?

                    S,
                    ALR

                    • 7. Re: EntityManager is null
                      Karen Greene Newbie

                      This code works except for the flush. The flush does not commit the updates to the database. Specifically, I want to update the status in the Fetcher.transfer method. However, the status does not get updated to "RUNNING". It only gets updated to "COMPLETE" once the fetcher is done. I thought maybe moving the entity manager outside the Watcher and Fetcher might fix this issue. So, I created a ConversionFileDAO which I tried to use in the code below, but I get a namenotfoundexception. I of course commented out the em method calls when I did this.

                      Watcher.java

                      @Stateless
                      @Clustered
                      public class Watcher
                      implements WatcherIntLocal, WatcherIntRemote
                      {

                      @Resource
                      private SessionContext context;
                      @PersistenceContext (unitName="WatcherDB")
                      private EntityManager em;


                      public void checkForNewFiles() throws WatcherException
                      {
                      try
                      {
                      List validFiles = findValidFiles();
                      addToQueue(validFiles);

                      }
                      catch (Throwable e)
                      {
                      e.printStackTrace();
                      throw new WatcherException(e);
                      }
                      }

                      public List findValidFiles() throws WatcherException
                      {
                      //iterate thru a list of files and search for certain extensions
                      //if the file name matches those extensions, call insert
                      }

                      public void insert(File file)
                      {
                      //// THIS DOES NOT WORK. get a namenotfoundexception
                      //InitialContext ctx = new InitialContext();
                      //ConversionFileDAORemote dao = (ConversionFileDAORemote) ctx.lookup("ConversionFileDAO/remote");
                      //dao.insert(file);

                      em.persist(file);

                      //FLUSH DOES NOT WORK. the file is persisted to the db, but only after the watcher is done. this causes the fetcher to get an exception b/c sometimes it can't find the entry in the db
                      em.flush();
                      }

                      public void addToQueue(List validFiles) throws WatcherException
                      {
                      // iterate thru files
                      //create a TextMessage using the file name
                      //send the message
                      }
                      }


                      ==================================
                      Fetcher.java

                      public class Fetcher
                      implements MessageListener
                      {

                      @Resource
                      private MessageDrivenContext context;

                      @PersistenceContext (unitName="WatcherDB")
                      private EntityManager em;

                      public void onMessage(Message arg0) {

                      String fileName = ((TextMessage)arg0).getText();
                      File remoteFile = (File)em.createQuery("from File where fileName=:filename").setParameter("filename",fileName).getSingleResult();

                      transfer(remotefile,localfile);

                      remoteFile.setStatus("COMPLETE");
                      em.merge(remoteFile);
                      em.flush();


                      }

                      public void transfer(File remoteFile, File localFile)
                      {

                      //// THIS DOES NOT WORK. get a namenotfoundexception
                      //InitialContext ctx = new InitialContext();
                      //ConversionFileDAORemote dao = (ConversionFileDAORemote) ctx.lookup("ConversionFileDAO/remote");
                      //dao.update(file);

                      remoteFile.setStatus("RUNNING");

                      em.merge(remoteFile);
                      //THIS DOES NOT WORK - status of running does not get updated to the db
                      em.flush();
                      }

                      }

                      =============================
                      public class ConversionFileDAO implements ConversionFileDAORemote
                      {

                      @PersistenceContext (unitName="WatcherDB")
                      private EntityManager em;

                      public insert(ConversionFile file)
                      {
                      em.persist(file);
                      em.flush();
                      }

                      public update(ConversionFile file)
                      {
                      em.merge(file);
                      em.flush();
                      }

                      public getConversionFile(String fileName)
                      {
                      ConversionFile currFile = null;

                      try {
                      currFile = (ConversionFile)em.createQuery("from ConversionFile where fileName=:filename").setParameter("filename", fileName).getSingleResult();
                      }
                      catch (NoResultException e1)
                      {

                      }

                      return file;
                      }
                      }