title: JPA EntityManager Static or Instance?
body: <p>I've built some JPA stuff in the past that used an instance of the <code>javax.persistence.EntityManager</code> per instance of DAO; this is the way most examples are setup. </p>
  
  <pre><code>public class BaseDaoThatEveryDaoExtends {
     @PersistenceContext
     private EntityManager entityManager;
  }
  </code></pre>
  
  <p>I've just stumbled upon code that uses a <strong>static</strong> <code>javax.peristence.EntityManger</code> injected by a <code>PersistenceContext</code> annotation, the architect tells me this causes no problems and they never had any problem even in a clustered application with JTA and an XA data source:</p>
  
  <pre><code>public class BaseDaoThatEveryDaoExtends {
     @PersistenceContext
     private static EntityManager entityManager;
  }
  </code></pre>
  
  <p>As far as I can tell this is an anti-pattern as the <code>EntityManager</code> holds some state information and making it static makes that whole state application wide. Also this makes the classes very hard to test. </p>
  
  <p>Are there other drawbacks to doing this or is this a standard way of using an <code>EntityManager</code> ?</p>