package com.iailab.framework.tenant.core.context;
|
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
import com.iailab.framework.common.enums.DocumentEnum;
|
|
/**
|
* 数据源上下文 Holder
|
*
|
* @author iailab
|
*/
|
public class DataContextHolder {
|
|
/**
|
* 数据源id
|
*/
|
private static final ThreadLocal<Long> DATA_SOURCE_ID = new TransmittableThreadLocal<>();
|
|
/**
|
* 数据源id
|
*
|
* @return 租户编号
|
*/
|
public static Long getDataSourceId() {
|
return DATA_SOURCE_ID.get();
|
}
|
|
/**
|
* 数据源id。如果不存在,则抛出 NullPointerException 异常
|
*
|
* @return 租户编号
|
*/
|
public static Long getRequiredDataSourceId() {
|
Long dataSourceId = getDataSourceId();
|
if (dataSourceId == null) {
|
throw new NullPointerException("DataContextHolder 不存在数据源id!可参考文档:"
|
+ DocumentEnum.TENANT.getUrl());
|
}
|
return dataSourceId;
|
}
|
|
public static void setDataSourceId(Long dataSourceId) {
|
DATA_SOURCE_ID.set(dataSourceId);
|
}
|
|
}
|