title: GWT and JPA concurrency, @PersistenceContext, NullPointerException
body: <p>I have a pure JAVA project, which I develop in eclipse with maven. It has a persistence capability using JPA with EclipseLink that save the data into Apache Derby. The project workts perfectly in unit tests and in standalone java applications, in which I instantiate the EntityManagerFactory directly from my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class JPAUtil
  {
      private static EntityManagerFactory     factory                 = Persistence.createEntityManagerFactory("unit-name");
      private static Map&lt;Long, EntityManager&gt; ems                     = new HashMap&lt;Long, EntityManager&gt;();
  
      private JPAUtil(){}
  
      /**
       * Get an entity manager
       */
      public static EntityManager em(Long id)
      {       
          EntityManager result = null;
          if (ems.containsKey(id))
          {
              result = ems.get(id);
              if(!result.isOpen())
              {
                  result = createEntityManager();
                  ems.put(id, result);
              }
          }
          else
          {
              result = createEntityManager();
              ems.put(id, result);
          }
  
          return result;
      }
  
      private static EntityManager createEntityManager()
      {
          EntityManager result =
                      // factory.createEntityManager(SynchronizationType.SYNCHRONIZED);
                      factory.createEntityManager();
          return result;
      }
  }
  </code></pre>
  
  <p>Now when I add it into a GWT project I am hitting some very difficult to debug/solve problems. </p>
  
  <p>Problem 1: </p>
  
  <p>If I use the above JPAUtil class to instantiate EntityManagers for use on each RPC request it works. However, if the GWT client-side started making multiple requests to the server-side, which in turn tried to pull data from the JPA layer, multiple cryptic ConcurrencyException occured on read (with or without lazy loading - it seems to make no difference).</p>
  
  <p>When instead of using the above class I try to "inject" the EntityManager using the following lines into the GWT ServiceImpls (servlets), attempting to access the data layer crashes with a NullPointerException:</p>
  
  <pre class="lang-java prettyprint-override"><code>@PersistenceContext(unitName = "unit-name")
  transient protected EntityManager   em;
  </code></pre>
  
  <p>I obviously was thinking, that it would be a more appropriate way of accessing the persistence layer from GWT. However, I get NullPointerExceptions when accessing the EntityManager, ergo the development Jetty server of GWT cannot inject the EntityManager by itself. My skills with this kind of problems appear to be limited, and my Google-FU seems to be helpless either. So to formulate a concrete question:</p>
  
  <p>How would it be best to approach the problem of creating a fast, stable GWT application with JPA in the backend?</p>
  
  <p>Thank you in advance,
  el.nicko</p>