Reputation: 23
I am New to SpringBoot.
I have a Spring Boot Application and the Database I am using is PostgreSQL, I am using JdbcTemplate and I have 2 datasource connections. The code works fine,but I observed that in PostgreSQL pgAdmin in the Server Status Dashboard,it shows the connection pool(Say 10 connection) in idle mode. While earlier I was working with single datasource, I observed the same thing,but I solved it by setting some properties within application.properties file. For.e.g:
spring.datasource.hikari.minimum-idle=somevalue
spring.datasource.hikari.idle-timeout=somevalue
How do I achieve the same with multiple Datasources.
properties file
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/stsdemo
spring.datasource.username=postgres
spring.datasource.password=********
spring.datasource.driver-class-name=org.postgresql.Driver
spring.seconddatasource.jdbcUrl=jdbc:postgresql://localhost:5432/postgres
spring.seconddatasource.username=postgres
spring.seconddatasource.password=********
spring.seconddatasource.driver-class-name=org.postgresql.Driver
DbConfig
@Configuration
public class DbConfig {
@Bean(name="db1")
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource firstDatasource()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcTemplate1")
public JdbcTemplate jdbcTemplate1(@Qualifier("db1") DataSource ds) {
return new JdbcTemplate(ds);
}
@Bean(name="db2")
@ConfigurationProperties(prefix="spring.seconddatasource")
public DataSource secondDatasource()
{
return DataSourceBuilder.create().build();
}
@Bean(name="jdbcTemplate2")
public JdbcTemplate jdbcTemplate2(@Qualifier("db2") DataSource ds)
{
return new JdbcTemplate(ds);
}
}
Upvotes: 1
Views: 3604
Reputation:
That's to be expected.
"closing" a connection (i.e. calling close()
) that is obtained from am pool will only return it to the pool. The pool will not immediately close the physical connection to the database to avoid costly reconnects (which is the whole point of using a connection pool)
An "idle" connection is also no real problem in Postgres.
If you have connections that stay "idle in transaction" for a long time - that would be a problem.
Upvotes: 3