fix:lambda的orElse是方法的话,会在执行lambda之前堆栈就调用了,那么Optional的逻辑就失去作用了,并且发现了一个逻辑问题,addDataSource没有使用到参数,会导致multiple逻辑有问题

This commit is contained in:
谨宸 2025-07-15 19:00:10 +08:00
parent 6e6f6de0f8
commit 08d6dae776
2 changed files with 10 additions and 9 deletions

View File

@ -36,6 +36,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
/** /**
@ -61,13 +62,13 @@ public class FlexDataSource extends AbstractDataSource {
this(dataSourceKey, dataSource, DbTypeUtil.getDbType(dataSource), needDecryptDataSource); this(dataSourceKey, dataSource, DbTypeUtil.getDbType(dataSource), needDecryptDataSource);
} }
public FlexDataSource(String dataSourceKey, DataSource dataSource, DbType dbType, boolean needDecryptDataSource){ public FlexDataSource(String dataSourceKey, DataSource dataSource, DbType dbType, boolean needDecryptDataSource) {
if (needDecryptDataSource) { if (needDecryptDataSource) {
DataSourceManager.decryptDataSource(dataSource); DataSourceManager.decryptDataSource(dataSource);
} }
// 处理dbType // 处理dbType
dbType = Optional.ofNullable(dbType).orElse(DbTypeUtil.getDbType(dataSource)); dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(dataSource));
this.defaultDataSourceKey = dataSourceKey; this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = dataSource; this.defaultDataSource = dataSource;
@ -88,8 +89,7 @@ public class FlexDataSource extends AbstractDataSource {
} }
// 优先取缓存否则根据数据源返回数据库类型 // 优先取缓存否则根据数据源返回数据库类型
DbType dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey)) DbType dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey)).orElseGet(() -> DbTypeUtil.getDbType(ds));
.orElse(DbTypeUtil.getDbType(ds));
this.defaultDataSourceKey = dataSourceKey; this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = ds; this.defaultDataSource = ds;
@ -104,13 +104,12 @@ public class FlexDataSource extends AbstractDataSource {
addDataSource(dataSourceKey, dataSource, DbTypeUtil.getDbType(dataSource), needDecryptDataSource); addDataSource(dataSourceKey, dataSource, DbTypeUtil.getDbType(dataSource), needDecryptDataSource);
} }
public void addDataSource(String dataSourceKey, DataSource dataSource, DbType dbType,boolean needDecryptDataSource) { public void addDataSource(String dataSourceKey, DataSource dataSource, DbType dbType, boolean needDecryptDataSource) {
if (needDecryptDataSource) { if (needDecryptDataSource) {
DataSourceManager.decryptDataSource(dataSource); DataSourceManager.decryptDataSource(dataSource);
} }
dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey)) dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(dataSource));
.orElse(DbTypeUtil.getDbType(dataSource));
dataSourceMap.put(dataSourceKey, dataSource); dataSourceMap.put(dataSourceKey, dataSource);
dbTypeHashMap.put(dataSourceKey, dbType); dbTypeHashMap.put(dataSourceKey, dbType);
@ -242,6 +241,7 @@ public class FlexDataSource extends AbstractDataSource {
/** /**
* 获取数据源缺失处理器 * 获取数据源缺失处理器
*
* @return DataSourceMissingHandler 数据源缺失处理器实例用于自定义处理逻辑记录日志抛出异常或提供默认数据源 * @return DataSourceMissingHandler 数据源缺失处理器实例用于自定义处理逻辑记录日志抛出异常或提供默认数据源
*/ */
public DataSourceMissingHandler getDataSourceMissingHandler() { public DataSourceMissingHandler getDataSourceMissingHandler() {

View File

@ -128,7 +128,8 @@ public class MultiDataSourceAutoConfiguration {
} }
// 如果没有构建成功dbType需要自解析 // 如果没有构建成功dbType需要自解析
dbType = Optional.ofNullable(dbType).orElse(DbTypeUtil.getDbType(dataSource)); final DataSource lambdaInnerDataSource = dataSource;
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(lambdaInnerDataSource));
if (flexDataSource == null) { if (flexDataSource == null) {
flexDataSource = new FlexDataSource(entry.getKey(), dataSource, dbType, false); flexDataSource = new FlexDataSource(entry.getKey(), dataSource, dbType, false);
} else { } else {