title: One session design pattern for EJB JPA and GWT RequestFactory aka keeping JPA session between multiple method calls
body: <p>The setup is like this:</p>
  
  <p><strong>Front-end:</strong>  <code>GWT</code> using <code>RequestFactory</code> for sending data objects<br>
  <strong>Back-end:</strong><br>
  <em>Web-layer:</em><br>
     <code>GWT</code> server side code which has injected EJB<br>
  <em>EJB-layer:</em><br>
  Stateless sesion beans:  </p>
  
  <p><strong>Data access bean</strong> (<code>DAB</code>)=> has injected <code>EntityManager</code> for <code>JPA</code> operations and provides methods for merging and retrieving of entities<br>
  <strong>Facade bean</strong> (<code>FB</code>) => calls methods of DAB and is interface between EJB and web layer</p>
  
  <p>When an entity object (lets say <code>MyEntity</code>) is to be saved after it has been modified at the client side, the flow is like this:<br>
  1. Initiated by the client<br>
  2. Server side <code>GWT</code> code is run and invokes the following methods:<br>
  3. The <code>find()</code> method looks up the instance of <code>MyEntity</code> using <code>FB.findMyEntity()</code> which calls <code>DAB.findMyEntity()</code> which in turn uses <code>EntityManager</code> to do the look-up. The <code>find()</code> method must be invoked as its part of <code>RequestFactory</code> flow in <code>GWT</code>.<br>
  4. The <code>save()</code> leads to <code>FB.saveMyEntity()</code> --> <code>DAB.saveMyEntity()</code> --> <code>EntityManager.merge()</code> and the changed entity object is persisted.</p>
  
  <p>It is obvious that each of <code>find()</code> and <code>save()</code> methods run in  different <code>JPA</code> transaction which is ineffective and bad design.  </p>
  
  <p>With regards to keeping the facade bean interface with simple look-up and saving methods:</p>
  
  <ol>
  <li>What is the best design to handle this situation? - preferably with having both method calls in one <code>JPA</code> transaction.</li>
  <li>What are alternatives? With pros and cons.</li>
  </ol>
  
  <p>EDIT: Including simplified examples of the code for <code>FB</code> and <code>DAB</code>. </p>
  
  <p>Facade bean (<code>FB</code>):</p>
  
  <pre><code>            @Stateless
              public class MyFacadeBean implements MyFacade{
  
                  @EJB
                  private DataAccessBean dab;
  
                  @Override
                  public void saveMyEntity(MyEntity entity) {
                      dab.saveMyEntity(entity);
                  }
  
                  @Override
                  public void findMyEntity(int id) {
                      dab.saveMyEntity(id);
                  }
  
              }
  </code></pre>
  
  <p>Data access bean (<code>DAB</code>):</p>
  
  <pre><code>        @Stateless
          public class DataAccesseBean implements DataAccessBeanInterface{
  
              @PersistenceContext
              private EntityManager entityManager;
  
              @Override
              public void saveMyEntity(MyEntity entity) {
                  entityManager.merge(entity);
              }
  
              @Override
              public void findMyEntity(int id) {
                  entityManager.find(MyEntity.class,id);
              }
  
          }
  </code></pre>