0

I am trying to figure it out how to configure a method not to run within a transaction using Spring. I have read that the Spring Data repositories by default activate the transactional behaviour in its methods. I dont want this transaction because I have many "save" calls to a repository and each of them is independent from the other. I think creating a transaction for each call to a repository method can slow down the code and the performance of the app. So :

  • Is this possible or every service or dao method has to run within a transaction?
  • If it has, why?
  • If this is possible, how to configure a method not to run within a transaction? Just removing the Spring transactional annotation?

Thanks

fernando1979
  • 1,727
  • 2
  • 20
  • 26

1 Answers1

0

Spring service beans by default are not transactional. You can add the @Transactional at a class or a method level to force it to be transactional. Here are a few links explaining in detail on how transactional in Spring works.
What is the difference between defining @Transactional on class vs method .
Spring - @Transactional - What happens in background? .
https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html#tx-decl-explained .

It is also discussed in the below thread .
Is Spring @Service transactional?

Arpan Kanthal
  • 475
  • 2
  • 7
  • But what about Spring Data repositories?. They are transactional annotated by default. How to override this behaviour? – fernando1979 Jul 17 '19 at 19:11
  • 1
    Yes, Spring Repositories add transactional behaviour by default to all crud queries. I am not entirely sure about the approach, i can suggest two solutions. One is to override the crud methods and exclude the @Transactional annotation, Two is to create a separate/new method which saves the data. Custom methods are not transactional by default inside a repository. Have a look at the answer from the below thread . https://stackoverflow.com/questions/39827054/spring-jpa-repository-transactionality – Arpan Kanthal Jul 17 '19 at 19:27