提交 4605c370 authored 作者: Matrix's avatar Matrix

feat(数据源): 增加了根据数据源名称切换当前使用的数据源

上级 6fd92fa2
...@@ -42,6 +42,13 @@ public interface IDataSourceService { ...@@ -42,6 +42,13 @@ public interface IDataSourceService {
@SuppressWarnings("UnusedReturnValue") @SuppressWarnings("UnusedReturnValue")
Set<String> switchDataSource(DataSourceDTO dto); Set<String> switchDataSource(DataSourceDTO dto);
/**
* 根据数据源名称切换数据源
* @param poolName 数据源名称
* @return 当前已经存在的数据源集合
*/
Set<String> switchDataSource(String poolName);
/** /**
* 添加数据源,如果已经存在同名的数据源了,则不会添加 * 添加数据源,如果已经存在同名的数据源了,则不会添加
* *
......
...@@ -86,12 +86,24 @@ public class IDataSourceServiceImpl implements IDataSourceService { ...@@ -86,12 +86,24 @@ public class IDataSourceServiceImpl implements IDataSourceService {
*/ */
@Override @Override
public Set<String> switchDataSource(DataSourceDTO dto) { public Set<String> switchDataSource(DataSourceDTO dto) {
// todo 该方法需要添加线程安全保护
Set<String> currentDataSources = add(dto); Set<String> currentDataSources = add(dto);
DynamicDataSourceContextHolder.push(dto.getPoolName()); DynamicDataSourceContextHolder.push(dto.getPoolName());
return currentDataSources; return currentDataSources;
} }
/**
* 根据数据源名称切换数据源
*
* @param poolName 数据源名称
* @return 当前已经存在的数据源集合
*/
@Override
public Set<String> switchDataSource(String poolName) {
DynamicDataSourceContextHolder.push(poolName);
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
return ds.getDataSources().keySet();
}
/** /**
* 添加数据源,如果已经存在同名的数据源了,则不会添加 * 添加数据源,如果已经存在同名的数据源了,则不会添加
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论