mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
fix:lambda的orElse是方法的话,会在执行lambda之前堆栈就调用了,那么Optional的逻辑就失去作用了,并且发现了一个逻辑问题,addDataSource没有使用到参数,会导致multiple逻辑有问题
This commit is contained in:
parent
6e6f6de0f8
commit
08d6dae776
@ -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);
|
||||||
@ -205,7 +204,7 @@ public class FlexDataSource extends AbstractDataSource {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Error resetting autoCommit to true before closing the connection. " +
|
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 数据源缺失处理器实例,用于自定义处理逻辑(如:记录日志、抛出异常或提供默认数据源)。
|
* @return DataSourceMissingHandler 数据源缺失处理器实例,用于自定义处理逻辑(如:记录日志、抛出异常或提供默认数据源)。
|
||||||
*/
|
*/
|
||||||
public DataSourceMissingHandler getDataSourceMissingHandler() {
|
public DataSourceMissingHandler getDataSourceMissingHandler() {
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user