title: JTA datasource, glassfish and eclipselink
body: <p>I'm maintaining some old code, that some not-so-clever developer once wrote (Me.), and when I tried to push a new update, suddenly it stopped working.</p>
  
  <p>Basically I have a BUNCH of DAO's which is used in a lot of different threads. All DAO's extend the class:</p>
  
  <pre><code>public class DAO {
  
      /**
       * The entitymanager.
       */
      private EntityManager entitymanager;
  
  
      /**
       * Gets the entitymanager
       *
       * @return the manager
       */
      public EntityManager getManager() {
          entitymanager = FactoryMaker.getFactory().createEntityManager();
          entitymanager.getTransaction().begin();
          entitymanager.flush();
          entitymanager.clear();
          return entitymanager;
  
      }
  
      /**
       * Commit and clear the manager.
       */
      public void Close() {
          if (entitymanager.isOpen()) {
              if (entitymanager.getTransaction().isActive()) {
                  entitymanager.getTransaction().commit();
                  entitymanager.clear();
              }
  
              entitymanager.close();
          }
      }
  
      @Override
      protected void finalize() throws Throwable {
          super.finalize();
      }
  
  }
  </code></pre>
  
  <p>So when a DAO is used, the following code is executed (e.g.)</p>
  
  <pre><code>    /**
   * Gets a profiletype based on the specified name.
   *
   * @param name the name
   * @return the by name
   */
  public Profiletype getByName(String name) {
  
      EntityManager manager = getManager();
      TypedQuery&lt;Profiletype&gt; query = manager.createQuery("SELECT w FROM Profiletype w WHERE w.name='" + name + "'", Profiletype.class);
  
      Profiletype result = query.getSingleResult();
      Close();
      return result;
  }
  </code></pre>
  
  <p>And this actually did the job. Well it's not pretty, but it worked. 
  I'm using glassfish to manage the connections, so my persistence.xml looks like:</p>
  
  <pre><code>&lt;persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
               xmlns="http://xmlns.jcp.org/xml/ns/persistence"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
      &lt;persistence-unit name="dtv4" transaction-type="JTA"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;jta-data-source&gt;jdbc/__default&lt;/jta-data-source&gt;
  
          &lt;class&gt;...&lt;/class&gt;
          &lt;class&gt;...&lt;/class&gt;
  
  
          &lt;properties&gt;
              &lt;property name="eclipselink.logging.level" value="INFO"/&gt;
              &lt;property name="eclipselink.ddl-generation" value="none"/&gt;
              &lt;property name="eclipselink.query-results-cache" value="false"/&gt;
              &lt;property name="eclipselink.refresh" value="true"/&gt;
  
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And I've actually just looked inside the running WAR, and It's the same persistence.xml and DAO class. But a quick google search shows that it's illegal (not possible!) to call <code>entitymanager.getTransaction().begin();</code> when using JTA. </p>
  
  <p>And of course, now I'm getting the error </p>
  
  <pre><code>java.lang.IllegalStateException: 
  Exception Description: Cannot use an EntityTransaction while using JTA.
  </code></pre>
  
  <p>Now my question is: How can I fix this, without rewriting everything? It's on my list, but right now I just want to push the update. I've read that you can use @PersistenceContext and such, but it doesn't seem to work, when my DAO is just a normal Java class. </p>
  
  <p>I hope some bright mind can help me. </p>
  
  <p>Best regards.</p>