You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/06/10 10:43:54 UTC

[GitHub] [shardingsphere] strongduanmu commented on issue #10699: default-database-strategy:hint hint can't use

strongduanmu commented on issue #10699:
URL: https://github.com/apache/shardingsphere/issues/10699#issuecomment-858515798


   Hi @stelin, I used the version `4.1.1` of `Sharding-JDBC` to test the Hint feature, and there is no problems. The following is my test demo:
   
   ```java
   public class HintTest {
       
       public static void main(String[] args) throws SQLException {
           DataSource dataSource = new HintTest().getShardingDataSource();
           Connection connection = dataSource.getConnection();
           PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM t_order");
           HintManager.getInstance().setDatabaseShardingValue(1);
           ResultSet resultSet = preparedStatement.executeQuery();
           while (resultSet.next()) {
               System.out.println(resultSet.getString("ORDER_ID") + " " + resultSet.getString("USER_ID"));
           }
           HintManager.getInstance().close();
           resultSet.close();
           preparedStatement.close();
           connection.close();
       }
       
       private DataSource getShardingDataSource() throws SQLException {
           ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
           shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
           shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
           shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
           shardingRuleConfig.getBroadcastTables().add("t_config");
           shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new HintShardingStrategyConfiguration(new AnnotationHintShardingAlgorithm()));
           shardingRuleConfig.setDefaultTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "ds_${order_id % 2}"));
           Properties props = new Properties();
           props.setProperty("sql.show", "true");
           return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, props);
       }
       
       private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
           KeyGeneratorConfiguration result = new KeyGeneratorConfiguration("SNOWFLAKE", "order_id");
           return result;
       }
       
       TableRuleConfiguration getOrderTableRuleConfiguration() {
           TableRuleConfiguration result = new TableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}");
           result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
           return result;
       }
       
       TableRuleConfiguration getOrderItemTableRuleConfiguration() {
           TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${0..1}");
           return result;
       }
       
       Map<String, DataSource> createDataSourceMap() {
           Map<String, DataSource> result = new HashMap<>();
           result.put("ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
           result.put("ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
           return result;
       }
   }
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org