提交 | 用户 | 时间
|
441d30
|
1 |
package com.iailab.framework.tenant.core.context; |
J |
2 |
|
|
3 |
import com.alibaba.ttl.TransmittableThreadLocal; |
|
4 |
import com.iailab.framework.common.enums.DocumentEnum; |
|
5 |
|
|
6 |
/** |
|
7 |
* 数据源上下文 Holder |
|
8 |
* |
|
9 |
* @author iailab |
|
10 |
*/ |
|
11 |
public class DataContextHolder { |
|
12 |
|
|
13 |
/** |
|
14 |
* 数据源id |
|
15 |
*/ |
|
16 |
private static final ThreadLocal<Long> DATA_SOURCE_ID = new TransmittableThreadLocal<>(); |
|
17 |
|
|
18 |
/** |
|
19 |
* 数据源id |
|
20 |
* |
|
21 |
* @return 租户编号 |
|
22 |
*/ |
|
23 |
public static Long getDataSourceId() { |
|
24 |
return DATA_SOURCE_ID.get(); |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* 数据源id。如果不存在,则抛出 NullPointerException 异常 |
|
29 |
* |
|
30 |
* @return 租户编号 |
|
31 |
*/ |
|
32 |
public static Long getRequiredDataSourceId() { |
|
33 |
Long dataSourceId = getDataSourceId(); |
|
34 |
if (dataSourceId == null) { |
|
35 |
throw new NullPointerException("DataContextHolder 不存在数据源id!可参考文档:" |
|
36 |
+ DocumentEnum.TENANT.getUrl()); |
|
37 |
} |
|
38 |
return dataSourceId; |
|
39 |
} |
|
40 |
|
|
41 |
public static void setDataSourceId(Long dataSourceId) { |
|
42 |
DATA_SOURCE_ID.set(dataSourceId); |
|
43 |
} |
|
44 |
|
|
45 |
} |