title: How does the UserTransaction and the EntityManager interact?
body: <p>This is an academic question; I have no broken code in relation to this.  I just want to expand my understanding of what is happening under the hood. </p>
  
  <p>The code pattern I have been using (copied from books and tutorials) in my JPA DAO for my typical JSF web applications is basically this:</p>
  
  <pre><code>public class someDAO {
  
      @PersistenceContext protected EntityManager   em;
      @Resource           private   UserTransaction utx;    
  
      public void persist(Entity entity) {
          try {
              utx.begin();
              em.persist(entity);
              utx.commit();
          } catch ( // gawd awful long list of possible exceptions ) 
  
          // etc
  </code></pre>
  
  <p>So my questions are as follows:</p>
  
  <ol>
  <li><p>Why are the EntityManager instance and the UserTransaction instance injected with annotations from two seemingly unrelated packages? </p></li>
  <li><p>Why is the annotation @Resource and @PersistenceContext rather than @ManagedProperty or perhaps @Inject used?</p></li>
  <li><p>How does the <strong>persist()</strong> method access and interact with the <strong>utx</strong> object?  If I forget the <strong>utx.begin()</strong> call the entity manager knows about it and throws and exception.  It must be finding the UserTransaction object in some magic way.   Wouldn't it have been better architecture to define the interface like:  <strong>em.persist(utx, entity)</strong>?</p></li>
  <li><p>If <strong>utx</strong> is some sort of singleton -- is it possible to have more than one UserTransaction open at a time?</p></li>
  </ol>
  
  <p>Much thanks for any discussion.</p>