I am working in Multi Tenant Spring Boot application. I am using the concept of one DB per Tenant approach. I have configured the dynamic database creation and connection. Also those database configurations are stored in a Map. Now I would like to implement an approach to clear DataSource which is idle for long time?
Asked
Active
Viewed 223 times
1
-
1hi, please read here -> https://stackoverflow.com/help/how-to-ask , and then edit your question :) – Leviand Jul 20 '18 at 10:45
-
instead of storing database connection details inside Map you can also use **another database table** which will provide dynamic connectivity based on `tenantId` and only 1 datasource will be loaded – bananas Aug 08 '19 at 05:56
-
@emotionlessbananas storing the details at anywhere is fine. But my requirement is how to find the particular datasource is idle for long time? – Alexpandiyan Chokkan Aug 08 '19 at 06:18
-
in that case you can look into `test-while-idle` and `validation-query` property – bananas Aug 08 '19 at 07:00
-
Do I have to initialize every datasource like the following answer https://stackoverflow.com/a/48413427/7458887 – Alexpandiyan Chokkan Aug 08 '19 at 07:07
-
no, you only have to call 1 database at run time. rest will be idle by default. This is SAAS based solution must look into it – bananas Aug 08 '19 at 09:12
-
Yeah It's a SAAS based application. We chose the one tenant per DB model. But how to use database resources efficiently? – Alexpandiyan Chokkan Aug 08 '19 at 16:51
1 Answers
1
I assume, you create Datasource and attempt to store them in a map,
whilst you create DS. you can mention
basicDataSource.setMinIdle(multitenantConnectionPoolConfig.getMinIdle());
So the connection pools ensure only active connections exist in the pool after the min Idle time expires.

Parthiban Manickam
- 147
- 8