Ask Your Question
0

How can sub database rules be loaded dynamically in shardingshpere-jdbc?

asked 2023-07-04 16:56:36 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-04 17:26:01 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-04 16:56:36 +0000

Seen: 14 times

Last updated: Jul 04 '23