Search in sources :

Example 6 with DynamicDataSourceModel

use of org.jeecg.common.system.vo.DynamicDataSourceModel in project kms by mahonelau.

the class DynamicDBUtil method getDbSourceByDbKey.

/**
 * 通过 dbKey ,获取数据源
 *
 * @param dbKey
 * @return
 */
public static DruidDataSource getDbSourceByDbKey(final String dbKey) {
    // 获取多数据源配置
    DynamicDataSourceModel dbSource = DataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
    // 先判断缓存中是否存在数据库链接
    DruidDataSource cacheDbSource = DataSourceCachePool.getCacheBasicDataSource(dbKey);
    if (cacheDbSource != null && !cacheDbSource.isClosed()) {
        log.debug("--------getDbSourceBydbKey------------------从缓存中获取DB连接-------------------");
        return cacheDbSource;
    } else {
        DruidDataSource dataSource = getJdbcDataSource(dbSource);
        if (dataSource != null && dataSource.isEnable()) {
            DataSourceCachePool.putCacheBasicDataSource(dbKey, dataSource);
        } else {
            throw new JeecgBootException("动态数据源连接失败,dbKey:" + dbKey);
        }
        log.info("--------getDbSourceBydbKey------------------创建DB数据库连接-------------------");
        return dataSource;
    }
}
Also used : JeecgBootException(org.jeecg.common.exception.JeecgBootException) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DynamicDataSourceModel(org.jeecg.common.system.vo.DynamicDataSourceModel)

Example 7 with DynamicDataSourceModel

use of org.jeecg.common.system.vo.DynamicDataSourceModel in project jeecg-boot by jeecgboot.

the class DataSourceCachePool method getCacheDynamicDataSourceModel.

/**
 * 获取多数据源缓存
 *
 * @param dbKey
 * @return
 */
public static DynamicDataSourceModel getCacheDynamicDataSourceModel(String dbKey) {
    String redisCacheKey = CacheConstant.SYS_DYNAMICDB_CACHE + dbKey;
    if (getRedisTemplate().hasKey(redisCacheKey)) {
        return (DynamicDataSourceModel) getRedisTemplate().opsForValue().get(redisCacheKey);
    }
    CommonAPI commonAPI = SpringContextUtils.getBean(CommonAPI.class);
    DynamicDataSourceModel dbSource = commonAPI.getDynamicDbSourceByCode(dbKey);
    if (dbSource != null) {
        getRedisTemplate().opsForValue().set(redisCacheKey, dbSource);
    }
    return dbSource;
}
Also used : CommonAPI(org.jeecg.common.api.CommonAPI) DynamicDataSourceModel(org.jeecg.common.system.vo.DynamicDataSourceModel)

Example 8 with DynamicDataSourceModel

use of org.jeecg.common.system.vo.DynamicDataSourceModel in project jeecg-boot by jeecgboot.

the class DynamicDBUtil method getDbSourceByDbKey.

/**
 * 通过 dbKey ,获取数据源
 *
 * @param dbKey
 * @return
 */
public static DruidDataSource getDbSourceByDbKey(final String dbKey) {
    // 获取多数据源配置
    DynamicDataSourceModel dbSource = DataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
    // 先判断缓存中是否存在数据库链接
    DruidDataSource cacheDbSource = DataSourceCachePool.getCacheBasicDataSource(dbKey);
    if (cacheDbSource != null && !cacheDbSource.isClosed()) {
        log.debug("--------getDbSourceBydbKey------------------从缓存中获取DB连接-------------------");
        return cacheDbSource;
    } else {
        DruidDataSource dataSource = getJdbcDataSource(dbSource);
        if (dataSource != null && dataSource.isEnable()) {
            DataSourceCachePool.putCacheBasicDataSource(dbKey, dataSource);
        } else {
            throw new JeecgBootException("动态数据源连接失败,dbKey:" + dbKey);
        }
        log.info("--------getDbSourceBydbKey------------------创建DB数据库连接-------------------");
        return dataSource;
    }
}
Also used : JeecgBootException(org.jeecg.common.exception.JeecgBootException) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DynamicDataSourceModel(org.jeecg.common.system.vo.DynamicDataSourceModel)

Aggregations

DynamicDataSourceModel (org.jeecg.common.system.vo.DynamicDataSourceModel)8 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 CommonAPI (org.jeecg.common.api.CommonAPI)3 JeecgBootException (org.jeecg.common.exception.JeecgBootException)3