public class TenantBroker
extends java.lang.Object
void methodA(){
// 因为某些特殊原因,需要手动指定租户
TenantContextHolder.setTenantId(1);
// do something ...
}
void methodB(){
// 因为某些特殊原因,需要手动指定租户
TenantContextHolder.setTenantId(2);
methodA();
// 此时租户ID已经变成 1
// do something ...
}
嵌套设置租户ID会导致租户上下文难以维护,并且很难察觉,容易导致数据错乱。 推荐的写法:
void methodA(){
TenantBroker.RunAs(1,() -> {
// do something ...
});
}
void methodB(){
TenantBroker.RunAs(2,() -> {
methodA();
// do something ...
});
}
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
TenantBroker.ApplyAs<T,R> |
static interface |
TenantBroker.RunAs<T> |
static class |
TenantBroker.TenantBrokerExceptionWrapper |
| 构造器和说明 |
|---|
TenantBroker() |
| 限定符和类型 | 方法和说明 |
|---|---|
<T> T |
applyAs(java.lang.Long tenant,
TenantBroker.ApplyAs<java.lang.Long,T> func)
以某个租户的身份运行
|
<T> T |
applyAs(java.util.function.Supplier<java.lang.Long> supplier,
TenantBroker.ApplyAs<java.lang.Long,T> func)
以某个租户的身份运行
|
void |
runAs(java.lang.Long tenant,
TenantBroker.RunAs<java.lang.Long> func)
以某个租户的身份运行
|
void |
runAs(java.util.function.Supplier<java.lang.Long> supplier,
TenantBroker.RunAs<java.lang.Long> func)
以某个租户的身份运行
|
public void runAs(java.lang.Long tenant,
TenantBroker.RunAs<java.lang.Long> func)
tenant - 租户IDfunc - public <T> T applyAs(java.lang.Long tenant,
TenantBroker.ApplyAs<java.lang.Long,T> func)
T - 返回数据类型tenant - 租户IDfunc - public void runAs(java.util.function.Supplier<java.lang.Long> supplier,
TenantBroker.RunAs<java.lang.Long> func)
supplier - func - public <T> T applyAs(java.util.function.Supplier<java.lang.Long> supplier,
TenantBroker.ApplyAs<java.lang.Long,T> func)
T - 返回数据类型supplier - func - Copyright © 2024 pig4cloud. All rights reserved.