title: Nested Transaction and EJBContext's setRollbackOnly()
body: <p>I am reading the Transaction Management of Java EE 7 and I get confused by the concept of nested transaction and the functionality of <code>EJBContext#setRollbackOnly()</code>. </p>
  
  <p>Say I have two Session Beans, <code>Bean1Impl</code> and <code>Bean2Impl</code> and their signatures are:</p>
  
  <pre><code>@Stateless 
  @TransactionManagement(TransactionManagementType.CONTAINER)
  public class Bean1Impl implements Bean1 {
  
      @Resource 
      private EJBContext context;
  
      @TransactionAttribute(REQUIRED)
      public void method1() {
          try {
              //some operations such as persist(), merge() or remove().
          }catch(Throwable th){
              context.setRollbackOnly();
          }
      }
  }
  
  @Stateless 
  @TransactionManagement(TransactionManagementType.CONTAINER)
  public class Bean2Impl implements Bean2 {
  
      @Resource 
      private EJBContext context;
  
      @TransactionAttribute(REQUIRED)
      public void method2() {
          try {
              //some operations such as persist(), merge() or remove().
              //an exception has been thrown
          }catch(Throwable th){
              context.setRollbackOnly();
          }
      }
  }
  </code></pre>
  
  <p>As stated in <a href="http://docs.oracle.com/javaee/7/tutorial/doc/transactions003.htm#JEETT00793" rel="nofollow">Java EE 7 Tutorial</a>:</p>
  
  <blockquote>
    <p><strong>51.3.1.1 Required Attribute</strong> </p>
    
    <p>If the client is running within a transaction and invokes the enterprise bean's method, the method
    executes within the client's transaction. If the client is not
    associated with a transaction, the container starts a new transaction
    before running the method.</p>
    
    <p>The Required attribute is the implicit transaction attribute for all
    enterprise bean methods running with container-managed transaction
    demarcation. You typically do not set the Required attribute unless
    you need to override another transaction attribute. Because
    transaction attributes are declarative, you can easily change them
    later.</p>
  </blockquote>
  
  <p>In this case I don't need to specify <code>@TransactionAttribute(REQUIRED)</code> annotation declaration in the methods <code>Bean1Impl#method1()</code> and <code>Bean2Impl#method2()</code>. Am I right?</p>
  
  <p>So in the above code the transaction of <code>Bean2Impl#method2()</code> would be running within the transaction of <code>Bean1Impl#method1()</code>. </p>
  
  <p>Can I consider it as a Nested Transaction?</p>
  
  <p>If there is an <code>Exception</code> has been thrown inside the method <code>Bean2Impl#method2()</code> which eventually would lead to a call to the method <code>EJBContext.setRollbackOnly()</code> from the <code>catch</code> block and as expected it should roll back the operations performed within the <code>try</code> block of this method. In this case what would happen to the transaction and as well as the <code>Bean1Impl#method1()</code>. Would it be rolled back as well? What I mean is:</p>
  
  <p>What would happen if there is a call of <code>EJBContext.setRollbackOnly()</code> from <code>Bean2Impl#method2()</code> and </p>
  
  <ul>
  <li><code>Bean2Impl#method2()</code> is called from the method <code>Bean1Impl#method1()</code> before any database operation like persist, merge or remove.</li>
  <li><code>Bean2Impl#method2()</code> is called from the method <code>Bean1Impl#method1()</code> after any database operation like persist, merge or remove.</li>
  </ul>
  
  <p>And lastly what would happen if the method <code>Bean2Impl#method2()</code> get executed successfully but <code>EJBContext.setRollbackOnly()</code> is called from <code>Bean1Impl#method1()</code> after successful return of <code>Bean2Impl#method2()</code>? </p>