title: Transaction management in Spring Boot and Spring data jpa
body: <p>Altough transaction management works Spring Data Repositories create their own transaction and suspend the active one.</p>
  
  <p>I have following Spring application:</p>
  
  <p>Application class:</p>
  
  <pre><code>@SpringBootApplication
  @EnableTransactionManagement
  public class SpringbootTxApplication {... }
  </code></pre>
  
  <p>Service class:</p>
  
  <pre><code>@Service
  public class EntityService {
      ...
      public void addEntityWithoutTransaction(MyEntity myEntity) {
          log.debug("addEntityWithoutTransaction start");
          myEntityRepository.save(myEntity);
          log.debug("addEntityWithoutTransaction end");
      }
  
      @Transactional
      public void addEntityTransaction(MyEntity myEntity) {
          log.debug("addEntityTransaction start");
          myEntityRepository.save(myEntity);
          log.debug("addEntityTransaction end");
      }
  }
  </code></pre>
  
  <p>While exeucting my EntityServiceTest which executes each method once and having spring transaction log in trace, I get following output:</p>
  
  <pre><code>... TRACE ... o.s.t.i.TransactionInterceptor           : Getting transaction for [de.miwoe.service.EntityService.addEntityTransaction]
  ... DEBUG ... de.miwoe.service.EntityService           : addEntityTransaction start
  ... TRACE ... o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
  ... TRACE ... o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
  ... DEBUG ... de.miwoe.service.EntityService           : addEntityTransaction end
  ... TRACE ... o.s.t.i.TransactionInterceptor           : Completing transaction for [de.miwoe.service.EntityService.addEntityTransaction]
  </code></pre>
  
  <p>And</p>
  
  <pre><code>... DEBUG ... de.miwoe.service.EntityService           : addEntityWithoutTransaction start
  ... TRACE ... o.s.t.i.TransactionInterceptor           : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
  ... TRACE ... o.s.t.i.TransactionInterceptor           : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
  ... DEBUG ... de.miwoe.service.EntityService           : addEntityWithoutTransaction end
  </code></pre>
  
  <p>Obviously according to the log, the @Transactional-Annotation is working in addEntityTransaction, but the repository still creates its own transaction. </p>
  
  <p>Why? Official docs (<a href="https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions" rel="noreferrer">https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions</a>) describe it should not begin a new one if it already exists.</p>
  
  <p>(Sometimes, Convention over Configuration seems more like Irritation over Convention over Configuration....)</p>
  
  <p>Am I missing something?</p>
  
  <p>(Complete code is also available here: <a href="https://github.com/miwoe/springboot-tx" rel="noreferrer">https://github.com/miwoe/springboot-tx</a>)</p>