mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: add DataSourceKey.use to support nested usage
This commit is contained in:
parent
c206540bf7
commit
e7292f53b4
@ -45,20 +45,30 @@ public class DataSourceKey {
|
||||
}
|
||||
|
||||
public static <T> T use(String dataSourceKey, Supplier<T> supplier) {
|
||||
String prevKey = manualKeyThreadLocal.get();
|
||||
try {
|
||||
use(dataSourceKey);
|
||||
manualKeyThreadLocal.set(dataSourceKey);
|
||||
return supplier.get();
|
||||
} finally {
|
||||
clear();
|
||||
if (prevKey != null) {
|
||||
manualKeyThreadLocal.set(prevKey);
|
||||
} else {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void use(String dataSourceKey, Runnable runnable) {
|
||||
String prevKey = manualKeyThreadLocal.get();
|
||||
try {
|
||||
use(dataSourceKey);
|
||||
manualKeyThreadLocal.set(dataSourceKey);
|
||||
runnable.run();
|
||||
} finally {
|
||||
clear();
|
||||
if (prevKey != null) {
|
||||
manualKeyThreadLocal.set(prevKey);
|
||||
} else {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -29,7 +29,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class MultiDataSourceTester {
|
||||
|
||||
@ -60,9 +59,9 @@ public class MultiDataSourceTester {
|
||||
AuditManager.setMessageCollector(collector);
|
||||
|
||||
Db.tx(() -> {
|
||||
Db.selectAll(null, "tb_account");
|
||||
Db.selectAll("tb_account");
|
||||
DataSourceKey.use("ds2");
|
||||
Db.selectAll(null, "tb_account");
|
||||
Db.selectAll("tb_account");
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user