\346\225\260\346\215\256\345\272\223\351\205\215\347\275\256\350\214\203\344\276\213\346\261\207\346\200\273.md
... ...
@@ -5,84 +5,84 @@
5 5
#### 数据库配置(文件dbcp.properties)如下
6 6
7 7
8
- ```
9
- ########DBCP##########
10
- driverClassName=com.mysql.cj.jdbc.Driver
11
- #url
12
- url=jdbc:mysql://ip:3306/iidp?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
13
- username=root
14
- password=123456
15
- initialSize=5
16
- maxActive=30
17
- minIdle=5
18
- maxWait=6000
19
- filters=stat
20
- timeBetweenEvictionRunsMillis=60000
21
- minEvictableIdleTimeMillis=300000
22
- validationQuery=select 'x'
23
- testOnBorrow=false
24
- testOnReturn=false
25
- testWhileIdle=true
26
- poolPreparedStatements: true
27
- maxOpenPreparedStatements: 20
28
-
29
- ```
8
+```properties
9
+########DBCP##########
10
+driverClassName=com.mysql.cj.jdbc.Driver
11
+#url
12
+url=jdbc:mysql://ip:3306/iidp?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
13
+username=username
14
+password=password
15
+initialSize=5
16
+maxActive=500
17
+minIdle=5
18
+maxWait=6000
19
+filters=stat
20
+timeBetweenEvictionRunsMillis=60000
21
+minEvictableIdleTimeMillis=300000
22
+validationQuery=SELECT 1
23
+testOnBorrow=false
24
+testOnReturn=false
25
+testWhileIdle=true
26
+poolPreparedStatements: true
27
+maxOpenPreparedStatements: 20
28
+keepAlive=true
29
+
30
+```
30 31
31 32
## 二 Oracle 配置范例
32 33
33 34
#### 数据库配置(文件dbcp.properties)如下
34 35
35 36
36
- ```
37
- ########Oracle########
38
- driverClassName=oracle.jdbc.OracleDriver
39
- url=jdbc:oracle:thin:@ip:1521:SMOMDB
40
- username=root
41
- password=123456
42
- validationQuery=SELECT 'x' FROM DUAL
43
-
44
- initialSize=5
45
- maxActive=30
46
- minIdle=5
47
- maxWait=6000
48
- filters=stat
49
- timeBetweenEvictionRunsMillis=60000
50
- minEvictableIdleTimeMillis=300000
51
- validationQuery=select 'x'
52
- testOnBorrow=false
53
- testOnReturn=false
54
- testWhileIdle=true
55
- poolPreparedStatements: true
56
- maxOpenPreparedStatements: 20
37
+```properties
38
+########Oracle########
39
+driverClassName=oracle.jdbc.OracleDriver
40
+url=jdbc:oracle:thin:@ip:1521:SMOMDB
41
+username=root
42
+password=123456
43
+validationQuery=SELECT 'x' FROM DUAL
44
+initialSize=5
45
+maxActive=30
46
+minIdle=5
47
+maxWait=6000
48
+filters=stat
49
+timeBetweenEvictionRunsMillis=60000
50
+minEvictableIdleTimeMillis=300000
51
+validationQuery=select 1 from dual
52
+testOnBorrow=false
53
+testOnReturn=false
54
+testWhileIdle=true
55
+poolPreparedStatements: true
56
+maxOpenPreparedStatements: 20
57 57
58
- ```
58
+```
59 59
60 60
61 61
## 三 PostgreSQL 配置范例
62 62
63 63
1. **DBCP 配置**
64 64
65
- ```properties
66
- ########postgresql########
67
- driverClassName=org.postgresql.Driver
68
- url=jdbc:postgresql://xxx.xxx.120:5432/postgres?currentSchema=public&encoding=UTF-8&timezone=UTC
69
- username=postgres
70
- password=******
71
-
72
- initialSize=5
73
- maxActive=30
74
- minIdle=5
75
- maxWait=6000
76
- filters=stat
77
- timeBetweenEvictionRunsMillis=60000
78
- minEvictableIdleTimeMillis=300000
79
- validationQuery=select 'x'
80
- testOnBorrow=false
81
- testOnReturn=false
82
- testWhileIdle=true
83
- poolPreparedStatements: true
84
- maxOpenPreparedStatements: 20
85
- ```
65
+```properties
66
+########postgresql########
67
+driverClassName=org.postgresql.Driver
68
+url=jdbc:postgresql://xxx.xxx.120:5432/postgres?currentSchema=public&encoding=UTF-8&timezone=UTC
69
+username=postgres
70
+password=******
71
+initialSize=5
72
+maxActive=500
73
+minIdle=5
74
+maxWait=6000
75
+filters=stat
76
+timeBetweenEvictionRunsMillis=60000
77
+minEvictableIdleTimeMillis=300000
78
+validationQuery=select 1
79
+testOnBorrow=false
80
+testOnReturn=false
81
+testWhileIdle=true
82
+poolPreparedStatements: true
83
+maxOpenPreparedStatements: 20
84
+keepAlive=true
85
+```
86 86
87 87
## 四 OceanBase 配置范例
88 88
... ...
@@ -90,11 +90,25 @@
90 90
91 91
**JDBC 连接示例**:
92 92
93
-```plaintext
93
+```properties
94 94
driverClassName=com.mysql.cj.jdbc.Driver
95 95
url=jdbc:oceanbase://xxx.xxx.xxx.xxx:3306/test?rewriteBatchedStatements=TRUE&allowMultiQueries=TRUE&useLocalSessionState=TRUE&useUnicode=TRUE&characterEncoding=utf-8&socketTimeout=10000&connectTimeout=30000
96 96
username=root
97 97
password=******
98
+initialSize=5
99
+maxActive=500
100
+minIdle=5
101
+maxWait=6000
102
+filters=stat
103
+timeBetweenEvictionRunsMillis=60000
104
+minEvictableIdleTimeMillis=300000
105
+validationQuery=select 1
106
+testOnBorrow=false
107
+testOnReturn=false
108
+testWhileIdle=true
109
+poolPreparedStatements: true
110
+maxOpenPreparedStatements: 20
111
+keepAlive=true
98 112
```
99 113
100 114
- **allowMultiQueries**:默认为 `FALSE`,必须设置为 `TRUE`,以允许使用分号连接的多语句文本格式。对使用文本协议的场景,只需开启此配置即可实现批量优化。
... ...
@@ -118,9 +132,10 @@ password=******
118 132
119 133
120 134
### 2. 本地 JDBC连接 OceanBase
135
+
121 136
dbcp.properties添加oceanbase配置
122 137
123
-```shell
138
+```properties
124 139
########OceanBase########
125 140
driverClassName=com.mysql.cj.jdbc.Driver
126 141
url=jdbc:mysql://xxx.xxx.xxx.xxx:2881/snest?rewriteBatchedStatements=TRUE&allowMultiQueries=TRUE&useLocalSessionState=TRUE&useUnicode=TRUE&characterEncoding=utf-8&socketTimeout=10000&connectTimeout=30000&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
... ...
@@ -135,10 +150,10 @@ validationQuery=SELECT 1
135 150
136 151
**1. dbcp.properties 示例:**
137 152
138
-```
153
+```properties
139 154
########DBCP##########
140 155
initialSize=5
141
-maxActive=2000
156
+maxActive=500
142 157
minIdle=5
143 158
filters=stat
144 159
maxWait=6000
... ...
@@ -183,26 +198,36 @@ Vastbase 海量数据库基本上兼容Postgres SQL,所以SQL语法和Postgres
183 198
184 199
### 1 默认兼容模型
185 200
186
-```
187
-vastbase数据库访问
188
-
201
+```properties
189 202
########DBCP##########
203
+initialSize=5
204
+maxActive=500
205
+minIdle=5
206
+filters=stat
207
+maxWait=6000
208
+timeBetweenEvictionRunsMillis=60000
209
+minEvictableIdleTimeMillis=1800000
210
+testOnBorrow=false
211
+testOnReturn=false
212
+testWhileIdle=true
213
+validationQuery=SELECT 1
214
+poolPreparedStatements: true
215
+maxOpenPreparedStatements: 20
216
+connectionProperties:druid.stat.sql.MaxSize=100;
217
+keepAlive=true
218
+
219
+########vastbase##########
190 220
driverClassName=cn.com.vastbase.Driver
191 221
url=jdbc:vastbase://ip:25432/snest?currentSchema=snest_changwen
192 222
username=root
193 223
password=******
194 224
195 225
196
-########snest_DBCP##########
197
-driverClassName=org.postgresql.Driver/cn.com.vastbase.Driver
198
-url=jdbc:vastbase://ip:25432/snest?currentSchema=snest_ziyang
199
-username=root
200
-password=******
201 226
```
202 227
203 228
### 2 MySQL兼容模型
204 229
205
-```
230
+```properties
206 231
vastbase数据库访问
207 232
数据库:snest_tpcc 端口:25434 用户名:snest_changwen
208 233
... ...
@@ -212,6 +237,7 @@ url=jdbc:vastbase://ip:25434/snest_tpcc?currentSchema=snest_changwen&db_compatib
212 237
username=root
213 238
password=******
214 239
dbCompatibility=mysql
240
+keepAlive=true
215 241
216 242
217 243
```
... ...
@@ -219,11 +245,27 @@ dbCompatibility=mysql
219 245
220 246
## 七 Dameng(国产达梦数据库) 配置范例
221 247
222
-
223 248
配置数据库连接信息
224 249
在 dbcp.properties 配置连接信息如下:
225 250
226
-```
251
+```properties
252
+########DBCP##########
253
+initialSize=5
254
+maxActive=500
255
+minIdle=5
256
+filters=stat
257
+maxWait=6000
258
+timeBetweenEvictionRunsMillis=60000
259
+minEvictableIdleTimeMillis=1800000
260
+testOnBorrow=false
261
+testOnReturn=false
262
+testWhileIdle=true
263
+validationQuery=select 1 from dual
264
+poolPreparedStatements: true
265
+maxOpenPreparedStatements: 20
266
+connectionProperties:druid.stat.sql.MaxSize=100;
267
+keepAlive=true
268
+
227 269
########dameng##########
228 270
driverClassName=dm.jdbc.driver.DmDriver
229 271
url=jdbc:dm://ip:30236/IIDP_DEMO
... ...
@@ -231,3 +273,143 @@ username=SYSDBA
231 273
password=SYSDBA001
232 274
```
233 275
276
+
277
+
278
+
279
+
280
+## 八 数据库连接池配置参数详解表
281
+
282
+### 📊 通用连接池配置参数(适用于所有数据库)
283
+
284
+| 参数名 | 默认值 | 推荐值 | 说明 | 影响 |
285
+| --------------------------------- | ------ | ------------ | --------------------- | ------------------------ |
286
+| **driverClassName** | - | 按数据库定 | JDBC驱动类全名 | 必须正确,否则无法连接 |
287
+| **url** | - | 按数据库定 | 数据库连接URL | 连接地址、端口、数据库名 |
288
+| **username** | - | - | 数据库用户名 | 认证信息 |
289
+| **password** | - | - | 数据库密码 | 认证信息 |
290
+| **initialSize** | 0 | 5-10 | 初始连接数 | 启动速度、初始性能 |
291
+| **maxActive** | 8 | 30-2000 | 最大活动连接数 | 最大并发处理能力 |
292
+| **maxIdle** | 8 | 同maxActive | 最大空闲连接数 | 资源占用 |
293
+| **minIdle** | 0 | 5-10 | 最小空闲连接数 | 保持"热"连接数量 |
294
+| **maxWait** | 无限 | 3000-10000ms | 获取连接最大等待时间 | 请求响应时间、超时控制 |
295
+| **validationQuery** | - | SELECT 1等 | 连接验证SQL | 连接有效性检查 |
296
+| **testOnBorrow** | true | false | 获取连接时验证 | 性能vs可靠性 |
297
+| **testOnReturn** | false | false | 归还连接时验证 | 性能开销 |
298
+| **testWhileIdle** | false | true | 空闲时验证 | 连接健康检查 |
299
+| **timeBetweenEvictionRunsMillis** | -1 | 60000ms | 回收线程运行间隔 | 连接回收频率 |
300
+| **minEvictableIdleTimeMillis** | 30min | 300000ms | 连接最小空闲时间 | 连接存活时间 |
301
+| **numTestsPerEvictionRun** | 3 | 3 | 每次检查连接数 | 回收效率 |
302
+| **poolPreparedStatements** | false | true | 缓存PreparedStatement | SQL执行性能 |
303
+| **maxOpenPreparedStatements** | -1 | 20-100 | 最大缓存PS数 | 内存占用 |
304
+| **removeAbandoned** | false | true | 移除泄露连接 | 防连接泄露 |
305
+| **removeAbandonedTimeout** | 300 | 180s | 泄露连接超时时间 | 连接泄露检测灵敏度 |
306
+| **logAbandoned** | false | true | 记录泄露日志 | 问题排查 |
307
+| **connectionProperties** | - | 各种优化参数 | 连接属性设置 | 性能优化 |
308
+
309
+---
310
+
311
+### 🔧 数据库特有配置
312
+
313
+#### 1. 连接验证SQL(validationQuery)
314
+
315
+| 数据库 | 推荐验证SQL | 说明 |
316
+| -------------- | ---------------------------------------------- | -------------- |
317
+| **MySQL** | `SELECT 1` | 最简单高效 |
318
+| **Oracle** | `SELECT 'x' FROM DUAL` 或 `SELECT 1 FROM DUAL` | 必须FROM DUAL |
319
+| **PostgreSQL** | `SELECT 1` | |
320
+| **SQL Server** | `SELECT 1` | |
321
+| **DB2** | `SELECT 1 FROM SYSIBM.SYSDUMMY1` | |
322
+| **达梦** | `SELECT 1 FROM DUAL` | 类似Oracle |
323
+| **GaussDB** | `SELECT 1` | |
324
+| **OceanBase** | `SELECT 1` | 兼容MySQL |
325
+| **Vastbase** | `SELECT 1` | 兼容PostgreSQL |
326
+
327
+#### 2. JDBC驱动类(driverClassName)
328
+
329
+| 数据库 | 驱动类 | 备注 |
330
+| -------------- | --------------------------------------------------------- | -------- |
331
+| **MySQL** | `com.mysql.cj.jdbc.Driver` | 8.0+用cj |
332
+| **Oracle** | `oracle.jdbc.OracleDriver` | |
333
+| **PostgreSQL** | `org.postgresql.Driver` | |
334
+| **SQL Server** | `com.microsoft.sqlserver.jdbc.SQLServerDriver` | |
335
+| **DB2** | `com.ibm.db2.jcc.DB2Driver` | |
336
+| **达梦** | `dm.jdbc.driver.DmDriver` | |
337
+| **GaussDB** | `com.huawei.gaussdb.jdbc.Driver` | |
338
+| **OceanBase** | `com.mysql.cj.jdbc.Driver` 或 `com.oceanbase.jdbc.Driver` | |
339
+| **Vastbase** | `cn.com.vastbase.Driver` 或 `org.postgresql.Driver` | |
340
+
341
+#### 3. URL格式示例
342
+
343
+| 数据库 | URL格式示例 |
344
+| -------------- | ------------------------------------------------------------ |
345
+| **MySQL** | `jdbc:mysql://host:3306/db?useUnicode=true&characterEncoding=UTF-8` |
346
+| **Oracle** | `jdbc:oracle:thin:@//host:1521/service_name` |
347
+| **PostgreSQL** | `jdbc:postgresql://host:5432/db?currentSchema=schema` |
348
+| **SQL Server** | `jdbc:sqlserver://host:1433;databaseName=db` |
349
+| **达梦** | `jdbc:dm://host:5236/database` |
350
+| **GaussDB** | `jdbc:gaussdb://host:8000/db` |
351
+| **OceanBase** | `jdbc:oceanbase://host:2881/db` |
352
+
353
+---
354
+
355
+### ⚡ 性能调优推荐配置
356
+
357
+#### 应用类型配置建议
358
+
359
+| 应用类型 | initialSize | maxActive | minIdle | 说明 |
360
+| -------------- | ------------ | --------- | -------- | ---------------- |
361
+| **小型应用** | 5 | 20 | 5 | 低并发,简单业务 |
362
+| **Web应用** | 10 | 50 | 10 | 中等并发 |
363
+| **高并发应用** | 20 | 200-2000 | 20 | 电商、交易系统 |
364
+| **批处理应用** | 根据批次大小 | 200-2000 | 根据需求 | 大数据处理 |
365
+| **微服务** | 5 | 30 | 5 | 多实例部署 |
366
+
367
+#### 关键参数调优表
368
+
369
+| 参数 | 调优目标 | 调优方法 | 监控指标 |
370
+| --------------------------------- | -------------- | ----------------------------- | ---------------- |
371
+| **maxActive** | 平衡并发与资源 | 监控连接使用率,设峰值的120% | ActiveCount峰值 |
372
+| **maxWait** | 减少超时 | 根据业务容忍时间设置 | 获取连接超时次数 |
373
+| **minEvictableIdleTimeMillis** | 减少创建开销 | 设为平均空闲时间的1.5倍 | 连接创建频率 |
374
+| **timeBetweenEvictionRunsMillis** | 及时回收 | 设为连接平均空闲时间的1/3 | 空闲连接数 |
375
+| **testWhileIdle** | 保证健康 | 必须开启,配合validationQuery | 连接失效次数 |
376
+
377
+---
378
+
379
+#### 🚨 环境监控参数
380
+
381
+```properties
382
+# 1. 连接泄露检测(必须)
383
+removeAbandoned=true
384
+removeAbandonedTimeout=180
385
+logAbandoned=true
386
+
387
+# 2. 连接健康检查(必须)
388
+testWhileIdle=true
389
+validationQuery=SELECT 1
390
+timeBetweenEvictionRunsMillis=30000
391
+
392
+# 3. 超时控制(必须)
393
+maxWait=10000
394
+connectionTimeout=30000
395
+socketTimeout=60000
396
+
397
+# 4. 监控统计(建议)
398
+filters=stat,wall
399
+connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=1000
400
+```
401
+
402
+---
403
+
404
+#### 📈 配置验证检查表
405
+
406
+| 检查项 | 标准 | 说明 |
407
+| -------------- | --------------------------------- | ---------------- |
408
+| **连接泄漏** | removeAbandoned=true | 防止连接未关闭 |
409
+| **连接验证** | testWhileIdle=true | 定期检查连接健康 |
410
+| **超时设置** | maxWait < 10000ms | 避免长时间等待 |
411
+| **验证SQL** | 数据库对应正确SQL | 确保验证有效 |
412
+| **连接数合理** | maxActive ≤ 数据库max_connections | 不超过数据库限制 |
413
+| **空闲连接** | minIdle ≥ 初始并发需求 | 保证基本性能 |
414
+
415
+这个表格可以作为所有数据库连接池配置的通用参考指南。
... ...
\ No newline at end of file