title: Spring transactions with caught Exceptions
body: <p>Recently, I've been working with Spring boot + spring data jpa + hibernate. I faced one problem with spring transactions. Here is my service class and two questions:</p>
  
  <pre><code>@Transactional
  @Service
  class MyService {
  
      @Autowired
      private MyRepository myRep;
  
      public void method_A() {
         try {
            method_C();
  
            .....
  
            method_B();
         } catch(Exception e) {}
      }
  
      public void method_B() {
         Entity e = new Entity();
         e.set(...);
  
         myRep.save(e);
      }
  
      public void method_C() throws Exception {
         ....
      }
  }
  </code></pre>
  
  <p>1.If method <code>method_C()</code> throws an Exception and I want to catch it and log it, the transaction is not rollbacked in method <code>method_B()</code>, because the Exception does not reach Spring framework.  So how should I do in order to catch Exceptions from <code>method_C()</code> and at the same time do not lose capability of method <code>method_B()</code> be rollbacked?</p>
  
  <p>2.Consider new method <code>method_A()</code>.</p>
  
  <pre><code>public void method_A() {
     for(...) {
        ...
        ...
        method_B();
     }
  }
  </code></pre>
  
  <p>I want invoke <code>method_B()</code> in a loop. If an exception occurs in a <code>method_B()</code> I want transaction of <code>method_B()</code> be rollbacked but <code>method_A()</code> should not exit and the loop should continue excuting. How can I achieve this?</p>