Java Code Examples for org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory

The following examples show how to use org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory. These examples are extracted from open source projects. 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 Project: java-tutorial   Source File: ShardingConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 获取数据源对象
 *
 * @throws SQLException
 */
@Bean
public DataSource shardingCreateDataSource() throws SQLException {
    Map<String, DataSource> dataSourceMap = getDataSourceMap();
    ShardingRuleConfiguration shardingRuleConfig = getShardingRuleConfig();
    DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());
    logger.info("------------------- The datasource has been created ---------------------------");
    return dataSource;
}
 
Example 2
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example 3
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example 4
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example 5
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example 6
Source Project: blog   Source File: DataSharding.java    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void main(String[] args) {
	// 配置真实数据源
	Map<String, DataSource> dataSourceMap = new HashMap<>();

	// 配置第一个数据源
	BasicDataSource dataSource1 = new BasicDataSource();
	dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
	dataSource1.setUrl("jdbc:mysql://localhost:3306/ds0");
	dataSource1.setUsername("root");
	dataSource1.setPassword("root");
	dataSourceMap.put("ds0", dataSource1);

	// 配置第二个数据源
	BasicDataSource dataSource2 = new BasicDataSource();
	dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
	dataSource2.setUrl("jdbc:mysql://localhost:3306/ds1");
	dataSource2.setUsername("root");
	dataSource2.setPassword("root");
	dataSourceMap.put("ds1", dataSource2);

	// 配置Order表规则
	TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order", "ds${0..1}.t_order${0..1}");

	// 配置分库 + 分表策略
	orderTableRuleConfig.setDatabaseShardingStrategyConfig(
			new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
	orderTableRuleConfig.setTableShardingStrategyConfig(
			new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));

	// 配置分片规则
	ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
	shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);

	// 省略配置order_item表规则...
	// ...

	// 获取数据源对象
	try {
		DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig,
				new Properties());
		Connection conn = dataSource.getConnection();
		String sql = "insert into t_order (user_id,order_id) values (?,?)";
		PreparedStatement preparedStatement = conn.prepareStatement(sql);
		for (int i = 1; i <= 10; i++) {
			preparedStatement.setInt(1, 10 + i);
			preparedStatement.setInt(2, 1001 + i);
			preparedStatement.executeUpdate();
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 
Example 7
/**
 * Get sharding data source bean.
 *
 * @return data source bean
 * @throws SQLException SQL exception
 */
@Bean
@Conditional(ShardingRuleCondition.class)
public DataSource shardingDataSource() throws SQLException {
    return ShardingDataSourceFactory.createDataSource(dataSourceMap, new ShardingRuleConfigurationYamlSwapper().swap(shardingProperties), propMapProperties.getProps());
}