Mark лет назад: 7
Родитель
Сommit
06e1dde8a5

+ 2 - 3
src/main/java/io/renren/datasources/DynamicDataSource.java

3
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
3
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
4
4
5
import javax.sql.DataSource;
5
import javax.sql.DataSource;
6
import java.util.HashMap;
7
import java.util.Map;
6
import java.util.Map;
8
7
9
/**
8
/**
15
public class DynamicDataSource extends AbstractRoutingDataSource {
14
public class DynamicDataSource extends AbstractRoutingDataSource {
16
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
15
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
17
16
18
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<String, DataSource> targetDataSources) {
17
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
19
        super.setDefaultTargetDataSource(defaultTargetDataSource);
18
        super.setDefaultTargetDataSource(defaultTargetDataSource);
20
        super.setTargetDataSources(new HashMap<>(targetDataSources));
19
        super.setTargetDataSources(targetDataSources);
21
        super.afterPropertiesSet();
20
        super.afterPropertiesSet();
22
    }
21
    }
23
22

+ 1 - 1
src/main/java/io/renren/datasources/DynamicDataSourceConfig.java

34
    @Bean
34
    @Bean
35
    @Primary
35
    @Primary
36
    public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) {
36
    public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) {
37
        Map<String, DataSource> targetDataSources = new HashMap<>();
37
        Map<Object, Object> targetDataSources = new HashMap<>();
38
        targetDataSources.put(DataSourceNames.FIRST, firstDataSource);
38
        targetDataSources.put(DataSourceNames.FIRST, firstDataSource);
39
        targetDataSources.put(DataSourceNames.SECOND, secondDataSource);
39
        targetDataSources.put(DataSourceNames.SECOND, secondDataSource);
40
        return new DynamicDataSource(firstDataSource, targetDataSources);
40
        return new DynamicDataSource(firstDataSource, targetDataSources);