We have a Spring Boot application, that heavily relies on SimpleJpaRepository implementations for performing database CRUD operations.
Problem: The class is annotated with @Transactional
, causing all methods called to be wrapped in an SQL transaction.
We would like our repositories to generate SQL transactions only for insert/update/delete operations, but not for select operations (unless they are called from another method annotated with @Transactional
)
How can we setup our repositories to achieve this, and what what would be the less obvious effects? (I assume the class was annotated like this for a reason)
Code and Logs
Calling @Transactional method, expected 1 transaction, found 1 (OK)
@Transactional
public User readUser(int id){
userRepository.findOne(id);
roleRepository.findByUser(id);
}
SQL Profiler:
set implicit_transactions on
exec sp_executesql N'select userentity0_.id as id1_45_, .....'
exec sp_executesql N'select roleassign0_.id as id1_36_, ....'
IF @@TRANCOUNT > 0 COMMIT TRAN
Without @Transactional, expected no transactions, found 2, created by the repositories (BAD):
public User readUser(int id){
userRepository.findOne(id);
roleRepository.findByUser(id);
}
SQL Profiler:
set implicit_transactions on
exec sp_executesql N'select userentity0_.id as id1_45_, .....'
IF @@TRANCOUNT > 0 COMMIT TRAN
set implicit_transactions on
exec sp_executesql N'select roleassign0_.id as id1_36_, ....'
IF @@TRANCOUNT > 0 COMMIT TRAN