Does JDBCTemplate use Connections Pool by default?

AR1 :

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?
user7294900 :

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();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103723&siteId=1