title: EJB 3.1 Singleton + JPA + JSF design advice needed
body: <p>Given: simple JSF webapp (no Seam), having JSF beans calling few EJB's which in turn load and persist JPA entities. What I want to is to use <code>@Singleton</code> annotation for ejb's and inject <code>EntityManager</code> <strong>instead of</strong> <code>EntityManagerFactory</code>:</p>
  
  <pre><code>@Singleton
  public class MyEJB {
    @PersistenceContext(unitName = PERSISTENCE_UNIT_NAME)
    protected EntityManager em; // not EntityManagerFactory
  }
  </code></pre>
  
  <p>Spec says that <code>@Singleton</code> is thread-safe, supports concurrency and transaction attributes which (from my pov) makes it safe for calling from JSF beans. I expect also performance benefits because of <code>EntityManager</code> not being recreated for each call and it's internal caching abilities. </p>
  
  <p>My main concern here is create/update operations on JPA entities in the situation when I have several singletons and, as a result, the same count of long-living EntityManagers.</p>
  
  <ul>
  <li>What happens if one singleton updates an JPA instance and how these
  changes are populated to other singletons?</li>
  <li>As I'm not able to close entity manager, do I need to flush it upon
  each entity update?</li>
  <li>Would it be better if these few singletons will share the same entity
  manager?</li>
  <li>I saw only few examples of such design. Why? Are there any serious
  drawbacks?</li>
  </ul>
  
  <p>Many thanks in advance!</p>