Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In ShardingSphere-JDBC, sub database rules can be dynamically loaded using the following steps:

  1. Create a new XML configuration file for the sub database rules. The file should contain the necessary database configuration details and the sub database rules.

  2. Use the ShardingSphere-JDBC API to load the new XML configuration file:

ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTables().add(new TableRuleConfiguration("table_name", "ds_name${0..1}.table_name_${0..1}"));
shardingRuleConfig.getBindingTableGroups().add("table_name");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloShardingDatabaseAlgorithm()));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloShardingTableAlgorithm()));
shardingRuleConfig.getShardingRuleExtensions().put("key", "value");
shardingRuleConfig.getBroadcastTables().add("table_name");
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(shardingRuleConfig), new Properties());
  1. Replace the existing ShardingSphere-JDBC configuration file with the newly created configuration file.

  2. Restart the application to apply the changes.