'How can I prevent @Transactional from holding onto connections from the data source that is not involved in the transaction?

I have 2 datasources in my SpringBoot app.

In one class without any annotation there is a method annotated with @Transactional which uses the second datasource, buy always gets a ConnectionHolder from primary dataset and holds its connection which is never used in this class.

.s.t.s.TransactionSynchronizationManager :  Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@6f9d4ae7] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@4c9bed65] bound to thread [http-nio-8080-exec-1]

.s.t.s.TransactionSynchronizationManager :  Bound value [org.springframework.jdbc.datasource.ConnectionHolder@55cec771] for key [HikariDataSource (HikariPool-sqlServer)] to thread [http-nio-8080-exec-1]

.s.t.s.TransactionSynchronizationManager :  Initializing transaction synchronization


Solution 1:[1]

Can you try setting transaction manager for the Transactional annotation you are using.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html#transactionManager--

This can help you to specify that use this transaction manager with this ds for this class.

Hope it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 swapyonubuntu