add
This commit is contained in:
@ -65,11 +65,12 @@ public class DatasourceConnectionMonitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.error("租户[{}]-目前主数据源异常, 开始切换备用数据源", tenantId);
|
log.error("租户[{}]-目前主数据源异常, 开始切换备用数据源", tenantId);
|
||||||
// 关闭原有异常数据源
|
|
||||||
connection.close();
|
connection.close();
|
||||||
dataSource.close();
|
|
||||||
// 主数据源异常 切换备用数据源
|
// 主数据源异常 切换备用数据源
|
||||||
changePrimary(tenantId);
|
if (changePrimary(tenantId)) {
|
||||||
|
// 备用切换成功, 关闭原有异常数据源
|
||||||
|
dataSource.close();
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -102,11 +103,12 @@ public class DatasourceConnectionMonitor {
|
|||||||
*
|
*
|
||||||
* @param tenantId 租户ID
|
* @param tenantId 租户ID
|
||||||
*/
|
*/
|
||||||
private void changePrimary(String tenantId) {
|
private Boolean changePrimary(String tenantId) {
|
||||||
// 备用数据源
|
// 备用数据源
|
||||||
List<DynamicDataSource> dataSources = DynamicDataSourceConfig.BACKUP_DATASOURCE_MAP.get(tenantId);
|
List<DynamicDataSource> dataSources = DynamicDataSourceConfig.BACKUP_DATASOURCE_MAP.get(tenantId);
|
||||||
if (Objects.isNull(dataSources)) {
|
if (Objects.isNull(dataSources)) {
|
||||||
log.error("租户:[{}]切换备用数据源失败, 原因: 没有备用数据源", tenantId);
|
log.error("租户:[{}]切换备用数据源失败, 原因: 没有备用数据源", tenantId);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer backupIndex = null;
|
Integer backupIndex = null;
|
||||||
@ -131,8 +133,10 @@ public class DatasourceConnectionMonitor {
|
|||||||
// 备用数据源集合删除该数据源
|
// 备用数据源集合删除该数据源
|
||||||
dataSources.remove((int) backupIndex);
|
dataSources.remove((int) backupIndex);
|
||||||
log.info("租户:[{}]切换备用数据源成功, 现主数据ID: {}", tenantId, dynamicDataSource.getDatasourceId());
|
log.info("租户:[{}]切换备用数据源成功, 现主数据ID: {}", tenantId, dynamicDataSource.getDatasourceId());
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
log.error("租户:[{}]切换备用数据源失败, 原因: 备用数据源均无效", tenantId);
|
log.error("租户:[{}]切换备用数据源失败, 原因: 备用数据源均无效", tenantId);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user