org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource Java Examples
The following examples show how to use
org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataSourceConfig.java From x7 with Apache License 2.0 | 6 votes |
@Lazy @Bean("routingDataSource") @Primary public AbstractRoutingDataSource routingDataSource(@Qualifier("dataSource") DataSource dataSource ,@Qualifier("readDataSource") DataSource readDataSource ) { AbstractRoutingDataSource proxy = new AbstractRoutingDataSource() { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.get(); } }; Map<Object,Object> map = new HashMap<>(); map.put(DataSourceType.READ,readDataSource); map.put(DataSourceType.WRITE,dataSource); proxy.setTargetDataSources(map); proxy.setDefaultTargetDataSource(dataSource); return proxy; }
Example #2
Source File: MybatisPlusConfig.java From MI-S with MIT License | 6 votes |
/** * 有多少个数据源就要配置多少个bean * @return */ @Bean public AbstractRoutingDataSource roundRobinDataSouceProxy() { int size = Integer.parseInt(dataSourceSize); MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size); Map<Object, Object> targetDataSources = new HashMap<Object, Object>(); // DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource"); //设置写库 targetDataSources.put(DataSourceType.write.getType(),dataSource); // targetDataSources.put(DataSourceType.read.getType(),readDataSource); //设置多读库 for (int i = 0; i < size; i++) { targetDataSources.put(i, readDataSources.get(i)); } proxy.setDefaultTargetDataSource(dataSource); proxy.setTargetDataSources(targetDataSources); return proxy; }
Example #3
Source File: DataSourceConfig.java From SpringBoot-Study with Apache License 2.0 | 6 votes |
/** * 注入AbstractRoutingDataSource * @param writeDataSource * @param read1DataSource * @param read2DataSource * @return * @throws Exception */ @Bean public AbstractRoutingDataSource routingDataSource( @Qualifier("writeDataSource") DataSource writeDataSource, @Qualifier("read1DataSource") DataSource read1DataSource, @Qualifier("read2DataSource") DataSource read2DataSource ) throws Exception { DynamicDataSource dataSource = new DynamicDataSource(); Map<Object, Object> targetDataSources = new HashMap(); targetDataSources.put(WRITE_DATASOURCE_KEY, writeDataSource); targetDataSources.put(READ1_DATASOURCE_KEY, read1DataSource); targetDataSources.put(READ2_DATASOURCE_KEY, read2DataSource); dataSource.setTargetDataSources(targetDataSources); dataSource.setDefaultTargetDataSource(writeDataSource); return dataSource; }
Example #4
Source File: MybatisConfiguration.java From javabase with Apache License 2.0 | 6 votes |
/** * 有多少个数据源就要配置多少个bean * @return */ @Bean(name = "roundRobinDataSouceProxy") public AbstractRoutingDataSource roundRobinDataSouceProxy() { int size = Integer.parseInt(dataSourceSize); MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size); Map<Object, Object> targetDataSources = new HashMap<Object, Object>(); DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource"); // 写 targetDataSources.put(DataSourceType.write.getType(), SpringContextHolder.getBean("writeDataSource")); for (int i = 0; i < size; i++) { targetDataSources.put(i, SpringContextHolder.getBean("readDataSource" + (i + 1))); } proxy.setDefaultTargetDataSource(writeDataSource); proxy.setTargetDataSources(targetDataSources); return proxy; }
Example #5
Source File: DataSourceConfig.java From x7 with Apache License 2.0 | 5 votes |
@Bean public PlatformTransactionManagerCustomizer platformTransactionManagerCustomizer(AbstractRoutingDataSource abstractRoutingDataSource){ return transactionManager -> { DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) transactionManager; dataSourceTransactionManager.setDataSource(abstractRoutingDataSource); }; }
Example #6
Source File: WriteOrReadDatabaseConfig.java From demo-project with MIT License | 5 votes |
/** * 设置数据源路由,通过该类中的determineCurrentLookupKey决定使用哪个数据源 */ @Bean public AbstractRoutingDataSource routingDataSource() { MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(); Map<Object, Object> targetDataSources = new HashMap<>(2); targetDataSources.put(DbContextHolder.WRITE, writeDataSource()); targetDataSources.put(DbContextHolder.READ+"1", read1()); proxy.setDefaultTargetDataSource(writeDataSource()); proxy.setTargetDataSources(targetDataSources); return proxy; }
Example #7
Source File: DataSourceConfig.java From SpringBoot-Study with Apache License 2.0 | 5 votes |
/** * 注入AbstractRoutingDataSource * @param readDruidDataSource * @param writeDruidDataSource * @return * @throws Exception */ @Bean public AbstractRoutingDataSource routingDataSource( @Qualifier(READ_DATASOURCE_KEY) DataSource readDruidDataSource, @Qualifier(WRITE_DATASOURCE_KEY) DataSource writeDruidDataSource ) throws Exception { DynamicDataSource dataSource = new DynamicDataSource(); Map<Object, Object> targetDataSources = new HashMap(); targetDataSources.put(WRITE_DATASOURCE_KEY, writeDruidDataSource); targetDataSources.put(READ_DATASOURCE_KEY, readDruidDataSource); dataSource.setTargetDataSources(targetDataSources); dataSource.setDefaultTargetDataSource(writeDruidDataSource); return dataSource; }
Example #8
Source File: DataSourceConfig.java From SuperBoot with MIT License | 5 votes |
/** * 构造包含多数据源的路由数据源实例类 * * @return * @throws Exception */ @Bean(name = "routingDataSource") public AbstractRoutingDataSource routingDataSource( ) throws Exception { DynamicDataSource dataSource = new DynamicDataSource(); Map<Object, Object> targetDataSources = new HashMap(); targetDataSources.put(WRITE_DATASOURCE_KEY, writeDruidDataSource); targetDataSources.put(READ_DATASOURCE_KEY, readDruidDataSource); dataSource.setTargetDataSources(targetDataSources); dataSource.setDefaultTargetDataSource(writeDruidDataSource); return dataSource; }