JDBC 任务
JDBC 任务是 Taskflow 中的一个专门系统任务,用于执行数据库操作。它提供了一个统一的接口来与各种关系型数据库进行交互,支持查询、更新、存储过程调用等操作。
核心特性
数据库操作
- SQL 查询执行
- 批量更新支持
- 存储过程调用
- 事务管理
连接管理
- 连接池优化
- 自动重连机制
- 负载均衡
- 故障转移
安全特性
- 参数化查询
- SQL 注入防护
- 访问控制
- 数据加密
任务参数
参数 | 描述 | 必需/可选 |
---|---|---|
query | SQL 查询语句或存储过程调用。支持参数化查询。 | 必需 |
parameters | 查询参数列表,用于替换查询中的占位符。 | 可选 |
dataSource | 数据源配置名称,用于指定要连接的数据库。 | 必需 |
timeout | 查询超时时间(秒)。默认为 60 秒。 | 可选 |
transaction | 事务配置,包括隔离级别和超时设置。 | 可选 |
配置示例
1. 基础查询
{
"name": "query_user",
"taskReferenceName": "query_user_ref",
"type": "JDBC",
"inputParameters": {
"dataSource": "mysql_users",
"query": "SELECT * FROM users WHERE id = ?",
"parameters": ["${workflow.input.userId}"]
}
}
2. 更新操作
{
"name": "update_status",
"taskReferenceName": "update_status_ref",
"type": "JDBC",
"inputParameters": {
"dataSource": "postgres_orders",
"query": "UPDATE orders SET status = ? WHERE order_id = ?",
"parameters": [
"${workflow.input.status}",
"${workflow.input.orderId}"
]
}
}
3. 存储过程调用
{
"name": "calc_statistics",
"taskReferenceName": "calc_stats_ref",
"type": "JDBC",
"inputParameters": {
"dataSource": "oracle_stats",
"query": "{call calculate_statistics(?, ?, ?)}",
"parameters": [
"${workflow.input.startDate}",
"${workflow.input.endDate}",
"${workflow.input.category}"
]
}
}
数据源配置
1. MySQL 配置
{
"name": "mysql_users",
"type": "mysql",
"url": "jdbc:mysql://localhost:3306/users",
"username": "app_user",
"password": "encrypted:xxx",
"properties": {
"maximumPoolSize": 10,
"minimumIdle": 2,
"connectionTimeout": 30000
}
}
2. PostgreSQL 配置
{
"name": "postgres_orders",
"type": "postgresql",
"url": "jdbc:postgresql://localhost:5432/orders",
"username": "app_user",
"password": "encrypted:xxx",
"properties": {
"ssl": true,
"sslMode": "verify-full",
"maximumPoolSize": 20
}
}
最佳实践
查询优化
- 使用参数化查询
- 避免全表扫描
- 合理使用索引
- 限制结果集大小
连接管理
- 使用连接池
- 及时释放连接
- 监控连接状态
- 设置合理超时
错误处理
- 实现重试机制
- 记录详细日志
- 提供回滚策略
- 处理死锁情况
性能优化
连接池配置
{ "properties": { "maximumPoolSize": 20, "minimumIdle": 5, "idleTimeout": 300000, "maxLifetime": 1200000, "connectionTimeout": 30000 } }
批量操作
{ "query": "INSERT INTO events (id, type, data) VALUES (?, ?, ?)", "batchParameters": [ ["id1", "type1", "data1"], ["id2", "type2", "data2"] ], "batchSize": 100 }
查询优化 ```sql
-- 使用索引 SELECT /*+ INDEX(users idx_user_id) */ * FROM users WHERE user_id = ?
-- 限制结果集 SELECT * FROM large_table WHERE condition = ? LIMIT 1000
## 错误处理
### 常见错误
1. **连接错误**
- 数据库不可用
- 认证失败
- 连接池耗尽
2. **查询错误**
- SQL 语法错误
- 约束违反
- 死锁
3. **超时错误**
- 查询执行超时
- 连接获取超时
- 事务超时
### 错误响应
```json
{
"status": "FAILED",
"error": "DATABASE_ERROR",
"message": "Failed to execute query",
"details": {
"errorCode": "40001",
"sqlState": "23505",
"message": "Unique constraint violation"
}
}
监控指标
性能指标
- 查询执行时间
- 连接获取时间
- 结果集大小
资源指标
- 活动连接数
- 等待连接数
- 连接池使用率
错误指标
- 查询失败率
- 连接失败率
- 超时次数
安全配置
访问控制
{ "permissions": { "read": ["app_read"], "write": ["app_write"], "admin": ["db_admin"] } }
数据加密
{ "encryption": { "enabled": true, "algorithm": "AES", "keyStore": "/path/to/keystore", "columns": ["ssn", "credit_card"] } }
调试技巧
SQL 日志
logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
连接跟踪
logging.level.com.zaxxer.hikari=DEBUG spring.datasource.hikari.leakDetectionThreshold=30000
UI 配置指南
数据源配置
- 配置连接信息
- 设置连接池参数
- 配置安全选项
任务配置
- 编写 SQL 查询
- 设置查询参数
- 配置超时和重试
监控面板
- 查看执行统计
- 监控连接状态
- 分析性能指标