fix:多数据源注解嵌套使用问题处理

This commit is contained in:
barql 2023-07-29 10:06:20 +08:00
parent 44aa1986c3
commit bd17ecb7df
2 changed files with 16 additions and 4 deletions

View File

@ -19,13 +19,24 @@ import java.util.function.Supplier;
public class DataSourceKey { public class DataSourceKey {
private static final ThreadLocal<String> keyThreadLocal = new ThreadLocal<>();
public static String manualKey;
private DataSourceKey() { private DataSourceKey() {
} }
private static final ThreadLocal<String> keyThreadLocal = new ThreadLocal<>();
public static void use(String dataSourceKey) { public static void use(String dataSourceKey) {
keyThreadLocal.set(dataSourceKey.trim()); keyThreadLocal.set(dataSourceKey.trim());
manualKey = dataSourceKey;
}
public static void use(String dataSourceKey, boolean isManual) {
if (isManual)
use(dataSourceKey);
else {
keyThreadLocal.set(dataSourceKey.trim());
}
} }
public static <T> T use(String dataSourceKey, Supplier<T> supplier) { public static <T> T use(String dataSourceKey, Supplier<T> supplier) {
@ -48,6 +59,7 @@ public class DataSourceKey {
public static void clear() { public static void clear() {
keyThreadLocal.remove(); keyThreadLocal.remove();
manualKey = null;
} }
public static String get() { public static String get() {

View File

@ -43,7 +43,7 @@ public class DataSourceInterceptor implements MethodInterceptor {
@Override @Override
public Object invoke(MethodInvocation invocation) throws Throwable { public Object invoke(MethodInvocation invocation) throws Throwable {
String dsKey = DataSourceKey.get(); String dsKey = DataSourceKey.manualKey;
if (StringUtil.isNotBlank(dsKey)) { if (StringUtil.isNotBlank(dsKey)) {
return invocation.proceed(); return invocation.proceed();
} }
@ -53,7 +53,7 @@ public class DataSourceInterceptor implements MethodInterceptor {
return invocation.proceed(); return invocation.proceed();
} }
DataSourceKey.use(dsKey); DataSourceKey.use(dsKey, false);
try { try {
return invocation.proceed(); return invocation.proceed();
} finally { } finally {