Overview
Sometimes you may need to use multiple databases in a Spring Boot application. You could read about how to set up multiple databases in a Spring Boot application in the previous post. In the post, we are going to describe how to use @Transactional with multiple databases in a spring boot application.
The usage of @Transactional with multiple databases
Firstly, we need to import @Transactional annotation from the following module:
1
import org.springframework.transaction.annotation.Transactional
Secondly, we have to use the Qualifier name to specify the transactional manager for a transaction:
1
2
3
4
5
6
7
8
9
10
@Transactional(FIRST_TRANSACTIONAL_MANAGER_QUALIFIER)
fun first() {
...
}
@Transactional(SECOND_TRANSACTIONAL_MANAGER_QUALIFIER)
fun second() {
...
}
If you use @Primary annotation on a first transactional manager then you don’t have to set FIRST_TRANSACTIONAL_MANAGER_QUALIFIER in @Transactional. It will be used by default.
Conclusion
We have described how to use @Transactional with multiple databases in a spring boot application.