Best Practices for the Druid Connection Pool
This section describes the core parameters and configuration examples of the Druid connection pool for TaurusDB application access. TaurusDB is compatible with the MySQL protocol. Applications can access TaurusDB using the standard MySQL JDBC driver. The core advantages of Druid lie in its built-in SQL monitoring, connection keepalive, and connection pool-level automatic reconnection.
Recommended Versions
| Component | Recommended Version | Description |
| Druid | 1.2.20 or later. 1.2.24 or a later stable version is recommended for production. | The keepAlive feature in versions earlier than 1.2.6 has defects. |
| MySQL JDBC driver | mysql-connector-j 8.0.33 or later | It is compatible with the TaurusDB 8.0 kernel protocol. |
| JDK | Java 8 or later | Druid 1.2.x supports Java 8 and later. |
Parameter Settings
| Parameter | Description | Example |
|---|---|---|
| url | JDBC connection URL | jdbc:mysql://host:3306/mydb |
| username | Database account | root |
| password | Account password | ****** |
| Parameter | Default Value | Description |
|---|---|---|
| initialSize | 0 | Number of physical connections created during initialization |
| minIdle | 0 | Minimum number of idle connections (lower limit for keepalive) |
| maxActive | 8 | Maximum number of active connections |
| maxWait | -1 | Maximum wait time (ms) to get a connection |
| Parameter | Default Value | Description | Recommended Value | Note |
|---|---|---|---|---|
| timeBetweenEvictionRunsMillis | 60000 | Eviction thread interval (ms) | 60000 (60s) | Decrease the value if you want to reclaim idle connections faster. Increase it if you are sensitive to database check load or thread overhead. |
| minEvictableIdleTimeMillis | 1800000 | Minimum idle time before eviction (ms) | 1800000 (30 min) | Decrease the value if you want to quickly release idle connections. Increase it if you want connections to persist to reduce the overhead of re-establishing connections. |
| maxEvictableIdleTimeMillis | 25200000 | Maximum idle time before forcible eviction (ms) | 25200000 (7 h) | Decrease this value to be lower than the database's wait_timeout if it is relatively small, preventing passive disconnection. Increase it if you want connections to be reused for a longer period. |
| keepAlive | false | Whether to send heartbeats to keep connections in minIdle alive | true | Set this parameter to true in primary/standby switchover or idle disconnection scenarios. Set it to false if the number of connections is small and creating a new connection each time is acceptable. |
| keepAliveBetweenTimeMillis | 120000 | Keepalive heartbeat interval (ms) | 120000 | Decrease this value to be lower than the database's wait_timeout if it is relatively small. Increase it if you are sensitive to check overhead. |
| validationQuery | None | Validation SQL statement, for example, SELECT 1 | SELECT 1 | This parameter is mandatory. Otherwise, checks such as testOnBorrow and testWhileIdle do not take effect. |
| validationQueryTimeout | -1 | Validation SQL timeout (s) | 3 (s) | Timeout interval of validationQuery. The default value is -1 (infinite waiting). Keeping the default value in the production environment is not recommended as it may block the validation thread. |
| testOnBorrow | false | Whether to validate on connection acquisition | false | Setting it to true has a significant impact on performance. Keeping it as false is recommended. |
| testOnReturn | false | Whether to validate on connection return | false | Generally, this function does not need to be enabled. The probability that a connection becomes invalid before being returned is extremely low. Enabling this function increases overheads. Consider enabling it only in environments with extremely poor connection quality. |
| testWhileIdle | true | Whether to validate idle connections | true | Minimum-overhead fallback health check. You are advised to set this parameter to true. |
Configuration Example
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://<taurusdb_vip>:3306/<db>?useUnicode=true&characterEncoding=utf8& &serverTimezone=Asia/Shanghai
username: <user>
password: <password>
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
keep-alive: true
keep-alive-between-time-millis: 120000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 1800000 What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot