!206 fix:多数据源嵌套使用问题

Merge pull request !206 from barql/272378774@qq.com
This commit is contained in:
Michael Yang 2023-07-29 02:36:53 +00:00 committed by Gitee
commit 0aa5443ccf
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
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 {