2

I set two parameters to disable auto commit by False but save operation on entity without transaction was committed.

spring.datasource.hikari.auto-commit=false
spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true

and the snippet code that I test the behavior is :

Log logEntity= new Log();
log.setId("123456789");
logRepository.save(logEntity);

after execute this code the logEntity saved in table.

How to disable auto commit flag in Spring boot?

Sam
  • 6,770
  • 7
  • 50
  • 91
  • You can use ```@Transactional``` on ```@Service``` level so you will have one transaction per one connection – Morph21 Mar 29 '22 at 12:43
  • I found an older stack question about that. Try this one https://stackoverflow.com/questions/55343616/how-to-disable-autocommit-spring-boot – Morph21 Mar 29 '22 at 12:44
  • I tried it but didn't work. – Sam Mar 30 '22 at 05:48
  • Did you find a solution? I have the exact same issue. I would like that Spring only commits when I explicitly set a `@Transactional` annotation. Without having to set `@Transactional(MANDATORY)` on every writing Repository method. – Ranil Wijeyratne May 08 '22 at 13:38

2 Answers2

5

I found the solution, I had to disable it on EnableJpaRepositories as following:

@EnableJpaRepositories(basePackages = {"org.company.product"},
                                       enableDefaultTransactions = false)
Sam
  • 6,770
  • 7
  • 50
  • 91
1

This didn't let me go. Apparently all methods of SimpleJpaRepository are annotated with @Transactional, which is the implementation for CrudRepositories and JpaRepository, and therefore commits after each call.

See: Why can I save without @Transactional?

Or directly: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions

Ranil Wijeyratne
  • 626
  • 7
  • 19