package com.qianwen.core.tenant.dynamic;
|
|
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
import java.util.Set;
|
import javax.sql.DataSource;
|
import com.qianwen.core.cache.utils.CacheUtil;
|
import com.qianwen.core.tenant.constant.TenantBaseConstant;
|
import com.qianwen.core.tool.utils.StringUtil;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
public class TenantDataSourceHolder {
|
private final DataSource dataSource;
|
private final DataSourceCreator dataSourceCreator;
|
private final JdbcTemplate jdbcTemplate;
|
|
public TenantDataSourceHolder(final DataSource dataSource, final DataSourceCreator dataSourceCreator, final JdbcTemplate jdbcTemplate) {
|
this.dataSource = dataSource;
|
this.dataSourceCreator = dataSourceCreator;
|
this.jdbcTemplate = jdbcTemplate;
|
}
|
|
public void handleDataSource(String tenantId) {
|
DynamicRoutingDataSource ds = (DynamicRoutingDataSource)this.dataSource;
|
Set<String> keys = ds.getCurrentDataSources().keySet();
|
if (!keys.contains(tenantId)) {
|
TenantDataSource tenantDataSource = getDataSource(tenantId);
|
if (tenantDataSource != null) {
|
DataSourceProperty dataSourceProperty = new DataSourceProperty();
|
BeanUtils.copyProperties(tenantDataSource, dataSourceProperty);
|
dataSourceProperty.setLazy(Boolean.FALSE);
|
DataSource dataSource = this.dataSourceCreator.createDataSource(dataSourceProperty);
|
ds.addDataSource(tenantId, dataSource);
|
}
|
}
|
}
|
|
private Boolean existDataSource(String tenantId) {
|
Boolean exist = (Boolean) CacheUtil.get(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_EXIST_KEY, tenantId, Boolean.class, Boolean.FALSE);
|
if (exist == null) {
|
TenantDataSource tenantDataSource = (TenantDataSource) this.jdbcTemplate.queryForObject(TenantBaseConstant.TENANT_DATASOURCE_EXIST_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper(TenantDataSource.class));
|
if (tenantDataSource != null && StringUtil.isNotBlank(tenantDataSource.getDatasourceId())) {
|
exist = Boolean.TRUE;
|
} else {
|
exist = Boolean.FALSE;
|
}
|
CacheUtil.put(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_EXIST_KEY, tenantId, exist, Boolean.FALSE);
|
}
|
return exist;
|
}
|
|
private TenantDataSource getDataSource(String tenantId) {
|
if (!existDataSource(tenantId).booleanValue()) {
|
return null;
|
}
|
TenantDataSource tenantDataSource = (TenantDataSource) CacheUtil.get(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_KEY, tenantId, TenantDataSource.class, Boolean.FALSE);
|
if (tenantDataSource == null) {
|
tenantDataSource = (TenantDataSource) this.jdbcTemplate.queryForObject(TenantBaseConstant.TENANT_DATASOURCE_SINGLE_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper(TenantDataSource.class));
|
if (tenantDataSource != null && StringUtil.isNoneBlank(new CharSequence[]{tenantDataSource.getTenantId(), tenantDataSource.getDriverClass(), tenantDataSource.getUrl(), tenantDataSource.getUsername(), tenantDataSource.getPassword()})) {
|
CacheUtil.put(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_KEY, tenantId, tenantDataSource, Boolean.FALSE);
|
} else {
|
tenantDataSource = null;
|
}
|
}
|
return tenantDataSource;
|
}
|
}
|