3 Replies Latest reply on Dec 11, 2007 3:29 PM by avogt_sundn

    Configuring persistence unit and accessing EntityManager via

    jsolderitsch

      I posted a related query to the Netbeans sub-forum but I see very little activity there. And maybe I should be using the persistence sub-forum.

      I have a project, originally developed in Netbeans 6 and deployed to Glassfish. I would like to deploy to JBoss 4.2.2 with as much of the code and configuration files as un-modified as possible.

      I am using JAX-WS annotations for the Web Service declarations and also am using the Java Persistence API (JPA).

      But I am not writing a EJB app -- just a web app, deploying with a .war file.

      I am using MySQL and I believe I have the mysql-ds.xml file as it should be. There I declare the JNDI name:

      <local-tx-datasource>
      <jndi-name>mysql_remote</jndi-name>


      My code relies on an EntityManager obtained via dependency injection like this:

      @WebService()
      @PersistenceContext(name = "persistence/LogicalName", unitName = "MissionStatusProjectPU2")
      public class MissionStatusService {

      EntityManager em;

      public MissionStatusService() {

      try {
      Context ctx = (Context) new javax.naming.InitialContext().lookup("java:comp/env");
      em = (EntityManager) ctx.lookup("persistence/LogicalName");
      } catch (NamingException ex) {
      ex.printStackTrace();
      }

      }
      ...

      My persistence properties are declared in a very simple persistence.xml file like:

      <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

      <persistence-unit name="MissionStatusProjectPU2" transaction-type="JTA">
      <jta-data-source>mysql_remote</jta-data-source>

      </persistence-unit>


      But in JBoss, I don't see how to further relate the MySQL data source (called mysql_remote) to an EntityManager.

      The code above leads to a run-time error for JBoss:

      17:44:54,134 ERROR [STDERR] javax.naming.NameNotFoundException: persistence not bound
      17:44:54,134 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
      17:44:54,134 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
      17:44:54,134 ERROR [STDERR] at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
      17:44:54,135 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
      17:44:54,135 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
      17:44:54,135 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
      17:44:54,135 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
      17:44:54,135 ERROR [STDERR] at com.gestalt.afmstt.modernization.MissionStatusService.(MissionStatusService.java:35)

      Line 35 is the ctx initialization line in the code fragment.

      This suggests to me that I need some additional resource properties defined somewhere (jboss-web.xml or web.xml perhaps) to link the EntityManager to the datasource.

      I confess I was spoiled by the config file automation that netbeans provides when dealing with glassfish.

      Can anyone suggest whether my approach is even possible for JBoss 4.2.2 or if there are known issues preventing this?

      If it is possible, what adjustments are needed in persistence.xml, jboss-web.xml and/or web.xml to allow me to overcome my current failure?

      I can provide further code details upon request.

        • 1. Re: Configuring persistence unit and accessing EntityManager
          jaikiran

           

          @WebService()
          @PersistenceContext(name = "persistence/LogicalName", unitName = "MissionStatusProjectPU2")
          public class MissionStatusService {

          EntityManager em;

          public MissionStatusService() {

          try {
          Context ctx = (Context) new javax.naming.InitialContext().lookup("java:comp/env");
          em = (EntityManager) ctx.lookup("persistence/LogicalName");
          } catch (NamingException ex) {
          ex.printStackTrace();
          }

          }


          How about this:

          @WebService()
          public class MissionStatusService {
          
          @PersistenceContext
          EntityManager em;
          
          public MissionStatusService() {
           //do nothing
          
          }
          .....
          }



          • 2. Re: Configuring persistence unit and accessing EntityManager
            jsolderitsch

             

            "jaikiran" wrote:
            @WebService()
            @PersistenceContext(name = "persistence/LogicalName", unitName = "MissionStatusProjectPU2")
            public class MissionStatusService {

            EntityManager em;

            public MissionStatusService() {

            try {
            Context ctx = (Context) new javax.naming.InitialContext().lookup("java:comp/env");
            em = (EntityManager) ctx.lookup("persistence/LogicalName");
            } catch (NamingException ex) {
            ex.printStackTrace();
            }

            }


            How about this:

            @WebService()
            public class MissionStatusService {
            
            @PersistenceContext
            EntityManager em;
            
            public MissionStatusService() {
             //do nothing
            
            }
            .....
            }



            I did try that.

            Then my web service handler that uses the em to create the response fails with a null pointer error for em.

            • 3. Re: Configuring persistence unit and accessing EntityManager
              avogt_sundn

              What happens if you use a @Stateless EJB?