Does JDBCTemplate use Connections Pool by default?
Asked Answered
A

1

8

I have a Spring Boot micro service that connects to several databases through a JDBC connection using JDBCTemplate:

@Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("mysqlDb") DataSource dsMySQL) {
        return new JdbcTemplate(dsMySQL);
    }

I have different template for each database and then rest template controller that chooses the right template to use depending on the parameters passed in the request. I read the documentation, however it's not clear:

  • Is a connection pool used out of the box or I need to specify it through configuration?
  • In this case is a connection pool used for each JDBCTemplate?
Aviva answered 9/10, 2018 at 12:14 Comment(2)
You need to configure your pool. Multiple templates use the same pool; not one to a customer unless you explicitly configure it to be so.Cordie
See Connection to a Production Database for more details.Scarcely
P
5

Spring Boot will try to load available connection pool for your DataSource:

Spring Boot uses the following algorithm for choosing a specific implementation:

We prefer HikariCP for its performance and concurrency. If HikariCP is available, we always choose it.

Otherwise, if the Tomcat pooling DataSource is available, we use it.

If neither HikariCP nor the Tomcat pooling datasource are available and if Commons DBCP2 is available, we use it. If you use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa “starters”, you automatically get a dependency to HikariCP.

You can bypass that algorithm completely and specify the connection pool to use by setting the spring.datasource.type property. This is especially important if you run your application in a Tomcat container, as tomcat-jdbc is provided by default.

Additional connection pools can always be configured manually. If you define your own DataSource bean, auto-configuration does not occur.

Example of defining your own bean:

@Bean
public DataSource dataSource() throws PropertyVetoException {
    return MyDataSourceHolder.getDataSource();
}
Petey answered 9/10, 2018 at 12:18 Comment(2)
Not sure what this ' If you define your own DataSource bean, auto-configuration does not occur.' meansAviva
@Aviva add example of defining your own beanPetey

© 2022 - 2024 — McMap. All rights reserved.