Search in sources :

Example 1 with GroupDataSource

use of com.baomidou.dynamic.datasource.ds.GroupDataSource in project dynamic-datasource-spring-boot-starter by baomidou.

the class DynamicRoutingDataSource method determinePrimaryDataSource.

private DataSource determinePrimaryDataSource() {
    log.debug("dynamic-datasource switch to the primary datasource");
    DataSource dataSource = dataSourceMap.get(primary);
    if (dataSource != null) {
        return dataSource;
    }
    GroupDataSource groupDataSource = groupDataSources.get(primary);
    if (groupDataSource != null) {
        return groupDataSource.determineDataSource();
    }
    throw new CannotFindDataSourceException("dynamic-datasource can not find primary datasource");
}
Also used : GroupDataSource(com.baomidou.dynamic.datasource.ds.GroupDataSource) CannotFindDataSourceException(com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException) GroupDataSource(com.baomidou.dynamic.datasource.ds.GroupDataSource) ItemDataSource(com.baomidou.dynamic.datasource.ds.ItemDataSource) P6DataSource(com.p6spy.engine.spy.P6DataSource) DataSource(javax.sql.DataSource) AbstractRoutingDataSource(com.baomidou.dynamic.datasource.ds.AbstractRoutingDataSource)

Example 2 with GroupDataSource

use of com.baomidou.dynamic.datasource.ds.GroupDataSource in project dynamic-datasource-spring-boot-starter by baomidou.

the class DynamicRoutingDataSource method addGroupDataSource.

/**
 * 新数据源添加到分组
 *
 * @param ds         新数据源的名字
 * @param dataSource 新数据源
 */
private void addGroupDataSource(String ds, DataSource dataSource) {
    if (ds.contains(UNDERLINE)) {
        String group = ds.split(UNDERLINE)[0];
        GroupDataSource groupDataSource = groupDataSources.get(group);
        if (groupDataSource == null) {
            try {
                groupDataSource = new GroupDataSource(group, strategy.getDeclaredConstructor().newInstance());
                groupDataSources.put(group, groupDataSource);
            } catch (Exception e) {
                throw new RuntimeException("dynamic-datasource - add the datasource named " + ds + " error", e);
            }
        }
        groupDataSource.addDatasource(ds, dataSource);
    }
}
Also used : GroupDataSource(com.baomidou.dynamic.datasource.ds.GroupDataSource) CannotFindDataSourceException(com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException)

Aggregations

GroupDataSource (com.baomidou.dynamic.datasource.ds.GroupDataSource)2 CannotFindDataSourceException (com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException)2 AbstractRoutingDataSource (com.baomidou.dynamic.datasource.ds.AbstractRoutingDataSource)1 ItemDataSource (com.baomidou.dynamic.datasource.ds.ItemDataSource)1 P6DataSource (com.p6spy.engine.spy.P6DataSource)1 DataSource (javax.sql.DataSource)1