mirror of https://gitee.com/y_project/RuoYi.git
Pre Merge pull request !427 from 尘埃落定/N/A
commit
d8e70a6075
|
@ -3,6 +3,8 @@ package com.ruoyi.common.config.datasource;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据源切换处理
|
* 数据源切换处理
|
||||||
*
|
*
|
||||||
|
@ -16,15 +18,17 @@ public class DynamicDataSourceContextHolder
|
||||||
* 使用ThreadLocal维护变量,ThreadLocal为每个使用该变量的线程提供独立的变量副本,
|
* 使用ThreadLocal维护变量,ThreadLocal为每个使用该变量的线程提供独立的变量副本,
|
||||||
* 所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
|
* 所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
|
||||||
*/
|
*/
|
||||||
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
|
private static final ThreadLocal<DataSourceTypeStruct> CONTEXT_HOLDER = new ThreadLocal<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置数据源的变量
|
* 设置数据源的变量
|
||||||
*/
|
*/
|
||||||
public static void setDataSourceType(String dsType)
|
public static void setDataSourceType(String dsType)
|
||||||
{
|
{
|
||||||
log.info("切换到{}数据源", dsType);
|
DataSourceTypeStruct dataSourceTypeStruct = Optional.ofNullable(CONTEXT_HOLDER.get()).orElse(new DataSourceTypeStruct(DataSourceType.MASTER.name()));//最顶层默认为 MASTER 数据源
|
||||||
CONTEXT_HOLDER.set(dsType);
|
dataSourceTypeStruct.setDsType(dsType);
|
||||||
|
log.info("切换到{}数据源,当前:{}", dsType,dataSourceTypeStruct);
|
||||||
|
CONTEXT_HOLDER.set(dataSourceTypeStruct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,7 +36,8 @@ public class DynamicDataSourceContextHolder
|
||||||
*/
|
*/
|
||||||
public static String getDataSourceType()
|
public static String getDataSourceType()
|
||||||
{
|
{
|
||||||
return CONTEXT_HOLDER.get();
|
DataSourceTypeStruct dataSourceTypeStruct = Optional.ofNullable(CONTEXT_HOLDER.get()).orElse(new DataSourceTypeStruct(DataSourceType.MASTER.name()));//最顶层默认为 MASTER 数据源
|
||||||
|
return dataSourceTypeStruct.getDsType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,6 +45,44 @@ public class DynamicDataSourceContextHolder
|
||||||
*/
|
*/
|
||||||
public static void clearDataSourceType()
|
public static void clearDataSourceType()
|
||||||
{
|
{
|
||||||
CONTEXT_HOLDER.remove();
|
DataSourceTypeStruct dataSourceTypeStruct = Optional.ofNullable(CONTEXT_HOLDER.get()).orElse(new DataSourceTypeStruct(DataSourceType.MASTER.name()));
|
||||||
|
dataSourceTypeStruct.clearDsType();
|
||||||
|
log.info("清理一层数据源,当前:{}", dataSourceTypeStruct);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 创建层级结构 保存数据源
|
||||||
|
*/
|
||||||
|
static class DataSourceTypeStruct{
|
||||||
|
private final String dsType;
|
||||||
|
private DataSourceTypeStruct next;
|
||||||
|
protected DataSourceTypeStruct(String dsType){
|
||||||
|
this.dsType = dsType;
|
||||||
|
}
|
||||||
|
public String getDsType(){//获得最底层DataSourceTypeStruct的dsType
|
||||||
|
if(null!=next){
|
||||||
|
return next.getDsType();
|
||||||
|
}
|
||||||
|
return dsType;
|
||||||
|
}
|
||||||
|
public void setDsType(String dsType){//设置最底层DataSourceTypeStruct的dsType
|
||||||
|
if(null!=next){
|
||||||
|
next.setDsType(dsType);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
next = new DataSourceTypeStruct(dsType);
|
||||||
|
}
|
||||||
|
public void clearDsType(){//清理最底层的DataSourceTypeStruct 层级-1
|
||||||
|
if(null!=next){
|
||||||
|
if(null==next.next){
|
||||||
|
next=null;
|
||||||
|
}else{
|
||||||
|
next.clearDsType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return dsType+(null==next?"":("->"+next));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue