簡介
HikariCP 來源於日語,「光」的意思,意味着它很快!可靠的數據源,spring boot2.0 已經將 HikariCP 做為了默認的數據源鏈接池。
官網詳細地說明了HikariCP所做的一些優化,總結如下:
- 字節碼精簡 :優化代碼,直到編譯后的字節碼最少,這樣,CPU緩存可以加載更多的程序代碼;
- 優化代理和攔截器:減少代碼,例如 HikariCP 的
Statement proxy
只有 100 行代碼,只有BoneCP 的十分之一; - 自定義數組類型(FastStatementList)代替 ArrayList:避免每次
get()
調用都要進行range check
,避免調用remove()
時的從頭到尾的掃描; - 自定義集合類型(ConcurrentBag):提高並發讀寫的效率;
詳細的介紹,可閱讀【追光者系列】Springboot 2.0選擇HikariCP作為默認數據庫連接池的五大理由
數據庫連接池
所有數據庫鏈接池都遵守基本的設計規則,實現 javax.sql.DataSource 接口,里面最重要的方法就是 Connection getConnection() throws SQLException; 用於獲取一個Connection, 一個Connection就是一個數據庫鏈接,就是一個TCP鏈接,建立TCP鏈接是需要進行3次握手的,這降低來鏈接的使用效率,也是各種數據庫鏈接池存在的原因。
數據庫鏈接池通過事先建立好 Connection
並緩存起來,這樣應用需要做數據查詢的時候,直接從緩存中拿到 Connection
就可以使用來。數據庫鏈接池還能夠檢測異常的鏈接,釋放閑置的鏈接。
B站-Java高級進階精講之高並發技術之理解數據庫連接池的原理與實現 介紹了數據庫連接池的概念,自己手動實現了一個簡易的數據庫連接池,雖然比較啰嗦,但是還是有內容的
常用屬性配置
如下屬性都不是必須的,屬於可選配置
autoCommit
This property controls the default auto-commit behavior of connections returned from the pool. It is a boolean value. Default: true
此屬性控制從池返回的連接的默認自動提交行為。 它是一個布爾值。 默認值:true
connectionTimeout
This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
此屬性控制客戶端(即您)等待池中連接的最大毫秒數。 如果在沒有連接可用的情況下超過此時間,則將拋出 SQLException
。 最低可接受的連接超時為 250 毫秒。 默認值:30000(30秒)
idleTimeout
This property controls the maximum amount of time that a connection is allowed to sit idle in the pool. This setting only applies when minimumIdle is defined to be less than maximumPoolSize. Idle connections will not be retired once the pool reaches minimumIdle connections. Whether a connection is retired as idle or not is subject to a maximum variation of +30 seconds, and average variation of +15 seconds. A connection will never be retired as idle before this timeout. A value of 0 means that idle connections are never removed from the pool. The minimum allowed value is 10000ms (10 seconds). Default: 600000 (10 minutes)
此屬性控制允許連接在池中空閑的最長時間。 此設置僅在 minimumIdle
定義為小於maximumPoolSize
時適用。 一旦池達到 minimumIdle
連接,空閑連接將不會退出。 連接是否空閑退出的最大變化為 +30 秒,平均變化為 +15 秒。 在此超時之前,連接永遠不會被空閑。 值為 0 表示永遠不會從池中刪除空閑連接。 允許的最小值為 10000 毫秒(10秒)。 默認值:600000(10分鍾)
maxLifetime
This property controls the maximum lifetime of a connection in the pool. An in-use connection will never be retired, only when it is closed will it then be removed. On a connection-by-connection basis, minor negative attenuation is applied to avoid mass-extinction in the pool. We strongly recommend setting this value, and it should be several seconds shorter than any database or infrastructure imposed connection time limit. A value of 0 indicates no maximum lifetime (infinite lifetime), subject of course to the idleTimeout setting. Default: 1800000 (30 minutes)
此屬性控制池中連接的最長生命周期。 使用中的連接永遠不會退役,只有當它關閉時才會被刪除。 在逐個連接的基礎上,應用輕微的負衰減以避免池中的大量滅絕。 我們強烈建議設置此值,它應比任何數據庫或基礎結構強加的連接時間限制短幾秒。 值 0 表示沒有最大生命周期(無限生命周期),當然主題是 idleTimeout
設置。 默認值:1800000(30分鍾)
connectionTestQuery
If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that do not support the JDBC4 Connection.isValid() API. This is the query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive. Again, try running the pool without this property, HikariCP will log an error if your driver is not JDBC4 compliant to let you know. Default: none
如果您的驅動程序支持JDBC4,我們強烈建議您不要設置此屬性。 這適用於不支持 JDBC4 Connection.isValid()API
的“遺留”驅動程序。 這是在從池中給出連接之前執行的查詢,以驗證與數據庫的連接是否仍然存在。 再次嘗試運行沒有此屬性的池,如果您的驅動程序不符合JDBC4,HikariCP將記錄錯誤以通知您。 默認值:無
minimumIdle
This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. If the idle connections dip below this value and total connections in the pool are less than maximumPoolSize, HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSize
此屬性控制 HikariCP 嘗試在池中維護的最小空閑連接數。 如果空閑連接低於此值並且池中的總連接數小於 maximumPoolSize
,則 HikariCP 將盡最大努力快速有效地添加其他連接。 但是,為了獲得最高性能和對峰值需求的響應,我們建議不要設置此值,而是允許 HikariCP 充當固定大小的連接池。 默認值:與 maximumPoolSize
相同
maximumPoolSize
This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend. A reasonable value for this is best determined by your execution environment. When the pool reaches this size, and no idle connections are available, calls to getConnection() will block for up to connectionTimeout milliseconds before timing out. Please read about pool sizing. Default: 10
此屬性控制允許池到達的最大大小,包括空閑和正在使用的連接。 基本上,此值將確定數據庫后端的最大實際連接數。 對此的合理值最好由您的執行環境決定。 當池達到此大小,並且沒有可用的空閑連接時,對 getConnection()
的調用將在超時前阻塞最多 connectionTimeout
毫秒。 請閱讀有關游泳池尺寸的信息。 默認值:10
metricRegistry
This property is only available via programmatic configuration or IoC container. This property allows you to specify an instance of a Codahale/Dropwizard MetricRegistry to be used by the pool to record various metrics. See the Metrics wiki page for details. Default: none
此屬性僅可通過編程配置或 IoC 容器獲得。 此屬性允許您指定池使用的 Codahale / Dropwizard MetricRegistry
的實例來記錄各種度量標准。 有關詳細信息,請參閱度量維基頁面。 默認值:無
healthCheckRegistry
This property is only available via programmatic configuration or IoC container. This property allows you to specify an instance of a Codahale/Dropwizard HealthCheckRegistry to be used by the pool to report current health information. See the Health Checks wiki page for details. Default: none
此屬性僅可通過編程配置或IoC容器獲得。 此屬性允許您指定池使用的 Codahale / Dropwizard HealthCheckRegistry
的實例來報告當前的健康信息。 有關詳細信息,請參閱運行狀況檢查維基頁面。 默認值:無
poolName
This property represents a user-defined name for the connection pool and appears mainly in logging and JMX management consoles to identify pools and pool configurations. Default: auto-generated
此屬性表示連接池的用戶定義名稱,主要顯示在日志記錄和 JMX 管理控制台中,以標識池和池配置。 默認值:自動生成
配置示例
Spring Boot 2.0 默認連接池就是 Hikari 了,所以引用 parents 后不用專門加依賴
- Spring Boot 2.x 默認使用 HikariCP
- Spring Boot 1.x 默認使用的是 Tomcat 連接池,需要移除
tomcat-jdbc
,配置spring.datasource.type=com.zaxxer.hikari.hIkari.HikariDatasource
例如:
spring.datasource.url=jdbc:mysql://localhost:3306/beta?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=cicd
spring.datasource.password=Home123*
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 可選配置
spring.datasource.type=com.zaxxer.hikari.hIkari.HikariDatasource
spring.datasource.hikari.minimumIdle=10
# Hikari連接池的設置
## Hikari 時間單位都是毫秒
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
## 連接池名字
spring.datasource.hikari.pool-name=MyHikariCP
## 最小空閑連接數量
spring.datasource.hikari.minimum-idle=10
## 空閑連接存活最大時間,默認600000(10分鍾)
spring.datasource.hikari.idle-timeout=600000
## 連接池最大連接數,默認是10
spring.datasource.hikari.maximum-pool-size=10
## 此屬性控制從池返回的連接的默認自動提交行為,默認值:true
spring.datasource.hikari.auto-commit=true
## 此屬性控制池中連接的最長生命周期,值0表示無限生命周期,默認1800000即30分鍾
spring.datasource.hikari.max-lifetime=1800000
## 數據庫連接超時時間,默認30秒,即30000
spring.datasource.hikari.connection-timeout=30000
訪問鏈接可以看到 acutator 監控數據:
代碼示例
FAQ
【追光者系列】Hikari連接池配多大合適?
參考
- 工匠小豬豬-總結 這位博主寫了 HikariCP 系列的文章,經典
- izhong-Hikari數據源介紹
- TinyZ-為什么使用HikariCP連接池?
- CSDN-HikariCP連接池及其在springboot中的配置