JDBCTemplate 是否默认使用连接池?
Does JDBCTemplate use Connections Pool by default?
我有一个 Spring 引导微服务,它使用 JDBC 模板通过 JDBC 连接连接到多个数据库:
@Bean(name = "mysqlJdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("mysqlDb") DataSource dsMySQL) {
return new JdbcTemplate(dsMySQL);
}
我对每个数据库都有不同的模板,然后休息模板控制器根据请求中传递的参数选择要使用的正确模板。
我阅读了文档,但不清楚:
- 连接池是开箱即用的还是需要通过配置指定?
- 在这种情况下,每个JDBC模板都使用一个连接池吗?
Spring 启动将尝试为您的数据源加载可用的 connection pool:
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.
定义您自己的 bean 的示例:
@Bean
public DataSource dataSource() throws PropertyVetoException {
return MyDataSourceHolder.getDataSource();
}
我有一个 Spring 引导微服务,它使用 JDBC 模板通过 JDBC 连接连接到多个数据库:
@Bean(name = "mysqlJdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("mysqlDb") DataSource dsMySQL) {
return new JdbcTemplate(dsMySQL);
}
我对每个数据库都有不同的模板,然后休息模板控制器根据请求中传递的参数选择要使用的正确模板。 我阅读了文档,但不清楚:
- 连接池是开箱即用的还是需要通过配置指定?
- 在这种情况下,每个JDBC模板都使用一个连接池吗?
Spring 启动将尝试为您的数据源加载可用的 connection pool:
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.
定义您自己的 bean 的示例:
@Bean
public DataSource dataSource() throws PropertyVetoException {
return MyDataSourceHolder.getDataSource();
}