- 出于tomcat数据源连接池的优秀性能和并发,如果可用总会优先使用它。
- 如果HikariCP可用,我们将使用它。
- 如果Commons DBCP可用,我们将使用它,但生产环境不推荐。
如果使用spring-boot-starter-jdbc
或spring-boot-starter-data-jpa
‘starters’,你会自动添加tomcat-jdbc
依赖。
注 通过指定spring.datasource.type
属性,你可以完全抛弃该算法,然后指定数据库连接池。如果你在tomcat容器中运行应用,由于默认提供tomcat-jdbc
,这就很重要了。
DataSource配置被外部的spring.datasource.*
属性控制,例如,你可能会在application.properties
中声明以下片段:
注 你应该至少使用属性指定url,或Spring Boot尝试自动配置内嵌数据库。
注 对于将要创建的池化DataSource
,我们需要验证是否有一个可用的Driver
,所以在做其他事前会校验它。比如,如果你设置spring.datasource.driver-class-name=com.mysql.jdbc.Driver
,然后该class加载出来,否则就会出错。
其他可选配置可以查看,有些标准配置是跟实现无关的,对于实现相关的配置可以通过相应前缀进行设置(spring.datasource.tomcat.*
,spring.datasource.hikari.*
,spring.datasource.dbcp.*
和),具体参考你使用的连接池文档。
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=50
# Validate the connection before borrowing it from the pool.