title: How many EntityManagers are injected for a given PersistenceContext?
body: <p>I am injecting <strong>EntityManager</strong> objects in stateless <strong>EJB3</strong> beans (which act as <strong>DAO</strong> objects and each provide access to a different database table). Deployment is in <strong>JBoss AS 7</strong>.</p>
  
  <p>I then added code using <strong>System.identityHashCode</strong> in the <strong>EJB3</strong> beans methods to see the various instances of the <strong>EntityManagers</strong> injecting (hoping to see the same instance in all DAOs). E.g. like:</p>
  
  <pre><code>@Stateless
  public class AFacade {
      @PersistenceContext(unitName="foo")
      EntityManager em;
  
      public List&lt;A&gt; findAll() {
           l.info("entity manager is: "+System.identityHashCode(em)+" class is: "+em.getClass().getSimpleName());
           ...
      }
  </code></pre>
  
  <p>However, what I noticed is that each <strong>DAO</strong> (e.g. <strong>AFacade</strong>, <strong>BFacade</strong> and so on) was injected with a different <strong>EntityManager</strong> (as reported by <strong>identityHashCode</strong>) although the <strong>PersistenceContext</strong> was the same. The implementing class was <strong>TransactionScopedEntityManager</strong> in all cases.</p>
  
  <p>It is not clear to me why this different <strong>EntityManager</strong> objects are injected, whether this is something that should concern me or not. Also, I understand that the EJB3 container may actually inject proxies to the real <strong>EntityManager</strong> so these different instances may actually be proxies to a single <strong>EntityManager</strong>.</p>