!552 fix:lambda的orElse是方法的话,会在执行lambda之前堆栈就调用了,那么Optional的逻辑就失去作用了,并且发现了一个逻…

Merge pull request !552 from 谨宸/main
This commit is contained in:
Michael Yang 2025-07-25 01:25:03 +00:00 committed by Gitee
commit 4d8c132b8d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
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.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
/**
@ -61,13 +62,13 @@ public class FlexDataSource extends AbstractDataSource {
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) {
DataSourceManager.decryptDataSource(dataSource);
}
// 处理dbType
dbType = Optional.ofNullable(dbType).orElse(DbTypeUtil.getDbType(dataSource));
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(dataSource));
this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = dataSource;
@ -88,8 +89,7 @@ public class FlexDataSource extends AbstractDataSource {
}
// 优先取缓存否则根据数据源返回数据库类型
DbType dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey))
.orElse(DbTypeUtil.getDbType(ds));
DbType dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey)).orElseGet(() -> DbTypeUtil.getDbType(ds));
this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = ds;
@ -104,13 +104,12 @@ public class FlexDataSource extends AbstractDataSource {
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) {
DataSourceManager.decryptDataSource(dataSource);
}
dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey))
.orElse(DbTypeUtil.getDbType(dataSource));
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(dataSource));
dataSourceMap.put(dataSourceKey, dataSource);
dbTypeHashMap.put(dataSourceKey, dbType);
@ -205,7 +204,7 @@ public class FlexDataSource extends AbstractDataSource {
} catch (SQLException e) {
if (log.isDebugEnabled()) {
log.debug("Error resetting autoCommit to true before closing the connection. " +
"Cause: " + e);
"Cause: " + e);
}
}
}
@ -242,6 +241,7 @@ public class FlexDataSource extends AbstractDataSource {
/**
* 获取数据源缺失处理器
*
* @return DataSourceMissingHandler 数据源缺失处理器实例用于自定义处理逻辑记录日志抛出异常或提供默认数据源
*/
public DataSourceMissingHandler getDataSourceMissingHandler() {

View File

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