GDAL优化

This commit is contained in:
2025-09-30 11:29:25 +08:00
parent 6b3509f1d9
commit 65048fbe89
5 changed files with 228 additions and 79 deletions

View File

@ -27,7 +27,7 @@ public class SQLiteUtil {
// 统一日期格式LocalDateTime
private static final DateTimeFormatter LOCAL_DATE_TIME_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 连接池缓存key=数据库文件路径value=DBCP2数据源支持多连接复用
// 连接池缓存key=数据库文件路径value=DBCP2数据源支持多连接复用
private static final Map<String, BasicDataSource> DATA_SOURCE_POOL = new ConcurrentHashMap<>();
// 字段缓存:缓存类的字段映射(避免反射重复开销)
@ -46,7 +46,7 @@ public class SQLiteUtil {
if (dbFilePath == null || dbFilePath.trim().isEmpty()) {
throw new IllegalArgumentException("数据库文件路径不能为空");
}
// 不存在则创建数据源存在则直接从池获取连接
// 不存在则创建数据源存在则直接从池获取连接
BasicDataSource dataSource = DATA_SOURCE_POOL.computeIfAbsent(dbFilePath, SQLiteUtil::createDataSource);
return dataSource.getConnection();
}
@ -63,7 +63,7 @@ public class SQLiteUtil {
dataSource.setDriverClassName("org.sqlite.JDBC");
dataSource.setUrl("jdbc:sqlite:" + dbFilePath);
// 2. 连接池核心参数(根据并发量调整SQLite不建议过多连接
// 2. 连接池核心参数(根据并发量调整SQLite不建议过多连接
dataSource.setMaxTotal(30); // 最大连接数20-50根据服务器CPU/内存调整)
dataSource.setMaxIdle(15); // 最大空闲连接:保留部分连接避免频繁创建
dataSource.setMinIdle(5); // 最小空闲连接:保证基础并发响应速度
@ -84,7 +84,7 @@ public class SQLiteUtil {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute("PRAGMA journal_mode=WAL;"); // 启用WAL模式支持多读者+单写者(核心优化)
stmt.execute("PRAGMA cache_size=-20000;"); // 页面缓存20MB负号表示KB单位内存足可调大)
stmt.execute("PRAGMA cache_size=-20000;"); // 页面缓存20MB负号表示KB单位内存足可调大)
stmt.execute("PRAGMA synchronous=NORMAL;"); // 同步级别平衡性能与安全崩溃最多丢WAL日志
stmt.execute("PRAGMA temp_store=MEMORY;"); // 临时表/索引存内存减少磁盘IO
stmt.execute("PRAGMA busy_timeout=2000;"); // 忙等待超时2秒避免瞬时并发锁等待
@ -165,7 +165,7 @@ public class SQLiteUtil {
throw new IllegalArgumentException("查询SQL不能为空");
}
// 预加载字段映射(缓存生效避免重复反射)
// 预加载字段映射(缓存生效避免重复反射)
Map<String, Field> fieldMap = getFieldMap(clazz);
// try-with-resources自动关闭Connection、PreparedStatement、ResultSet
@ -175,7 +175,7 @@ public class SQLiteUtil {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 预构建「列名-字段」映射(一次构建循环复用)
// 预构建「列名-字段」映射(一次构建循环复用)
ColumnFieldMapping[] mappings = buildColumnFieldMappings(metaData, columnCount, fieldMap);
// 处理结果集(反射赋值)
@ -254,7 +254,7 @@ public class SQLiteUtil {
*
* @param dbFilePath 数据库路径
* @param sql DDL语句
* @param params SQL参数可选如动态表名)
* @param params SQL参数可选如动态表名)
*/
public static void executeDDL(String dbFilePath, String sql, List<Object> params) throws SQLException {
if (dbFilePath == null || dbFilePath.trim().isEmpty()) {
@ -269,7 +269,7 @@ public class SQLiteUtil {
pstmt.execute();
} catch (SQLException e) {
closeDataSource(dbFilePath);
throw new SQLException("执行DDL失败SQL" + sql + "路径:" + dbFilePath + "", e);
throw new SQLException("执行DDL失败SQL" + sql + "路径:" + dbFilePath + "", e);
}
}
@ -284,7 +284,7 @@ public class SQLiteUtil {
// ========================== 工具辅助方法 ==========================
/**
* 创建PreparedStatement复用Connection避免重复获取)
* 创建PreparedStatement复用Connection避免重复获取)
*
* @param conn 已获取的Connection从连接池来
* @param sql SQL语句
@ -362,7 +362,7 @@ public class SQLiteUtil {
}
/**
* 获取类的字段缓存(含父类字段一次反射永久缓存)
* 获取类的字段缓存(含父类字段一次反射永久缓存)
*
* @param clazz 目标类
* @return 字段名→Field的映射不可修改
@ -385,7 +385,7 @@ public class SQLiteUtil {
}
/**
* 优化的字段赋值(处理类型转换避免重复异常捕获)
* 优化的字段赋值(处理类型转换避免重复异常捕获)
*
* @param obj 目标对象
* @param field 字段
@ -407,7 +407,7 @@ public class SQLiteUtil {
field.set(obj, convertedValue);
}
} catch (IllegalAccessException e) {
System.err.println("警告:字段赋值失败(字段:" + field.getName() + "值类型:" + value.getClass().getName() + "" + e.getMessage());
System.err.println("警告:字段赋值失败(字段:" + field.getName() + "值类型:" + value.getClass().getName() + "" + e.getMessage());
}
}
@ -451,7 +451,7 @@ public class SQLiteUtil {
}
// 不支持的类型转换(打印警告)
System.err.println("警告:不支持的类型转换(目标类型:" + targetType.getName() + "原始值类型:" + value.getClass().getName() + "");
System.err.println("警告:不支持的类型转换(目标类型:" + targetType.getName() + "原始值类型:" + value.getClass().getName() + "");
return null;
}
@ -547,7 +547,7 @@ public class SQLiteUtil {
// ========================== 内部辅助类 ==========================
/**
* 列-字段映射模型(一次性构建减少循环内计算)
* 列-字段映射模型(一次性构建减少循环内计算)
*/
private static class ColumnFieldMapping {
String columnName; // 数据库列名