mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
!206 fix:多数据源嵌套使用问题
Merge pull request !206 from barql/272378774@qq.com
This commit is contained in:
commit
0aa5443ccf
@ -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() {
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user