Retro Eye care Haitian Deep Dark Default

Hint

Background

Apache ShardingSphere uses ThreadLocal to manage sharding key values for mandatory routing. A sharding value can be added by programming to the HintManager that takes effect only within the current thread.

Main application scenarios for Hint:

  • The sharding fields do not exist in the SQL and database table structure but in the external business logic.
  • Certain data operations are forced to be performed in given databases.

Procedure

  1. Call HintManager.getInstance() to obtain an instance of HintManager.
  2. Use HintManager.addDatabaseShardingValue, HintManager.addTableShardingValue to set the sharding key value.
  3. Execute SQL statements to complete routing and execution.
  4. Call HintManager.close to clean up the contents of ThreadLocal.

Sample

Hint Configuration

Hint algorithms require users to implement the interface of org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm. org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm has two built-in implementations,

  • org.apache.shardingsphere.sharding.algorithm.sharding.hint.HintInlineShardingAlgorithm
  • org.apache.shardingsphere.sharding.algorithm.sharding.classbased.ClassBasedShardingAlgorithm

Apache ShardingSphere will acquire sharding values from HintManager to route.

Take the following configurations for reference:

rules:
  - !SHARDING
    tables:
      t_order:
        actualDataNodes: demo_ds_${0..1}.t_order_${0..1}
        databaseStrategy:
          hint:
            shardingColumn: order_id
            shardingAlgorithmName: hint_class_based
        tableStrategy:
          hint:
            shardingColumn: order_id
            shardingAlgorithmName: hint_inline
    shardingAlgorithms:
      hint_class_based:
        type: CLASS_BASED
        props:
          strategy: STANDARD
          algorithmClassName: xxx.xxx.xxx.HintXXXAlgorithm
      hint_inline:
        type: HINT_INLINE
        props:
          algorithm-expression: t_order_$->{value % 4}
    defaultTableStrategy:
      none:
    defaultKeyGenerateStrategy:
      type: SNOWFLAKE
      column: order_id

props:
  sql-show: true

Get HintManager

HintManager hintManager = HintManager.getInstance();

Add Sharding Value

  • Use hintManager.addDatabaseShardingValue to add sharding key value of data source.
  • Use hintManager.addTableShardingValue to add sharding key value of table.

Users can use hintManager.setDatabaseShardingValue to set sharding value in hint route to some certain sharding database without sharding tables.

Clean Hint Values

Sharding values are saved in ThreadLocal, so it is necessary to use hintManager.close() to clean ThreadLocal.

HintManager has implemented AutoCloseable. We recommend to close it automatically with try with resource.

Codes:

// Sharding database and table with HintManager
String sql = "SELECT * FROM t_order";
try (HintManager hintManager = HintManager.getInstance();
     Connection conn = dataSource.getConnection();
     PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
    hintManager.addDatabaseShardingValue("t_order", 1);
    hintManager.addTableShardingValue("t_order", 2);
    try (ResultSet rs = preparedStatement.executeQuery()) {
        while (rs.next()) {
            // ...
        }
    }
}

// Sharding database and one database route with HintManager
String sql = "SELECT * FROM t_order";
try (HintManager hintManager = HintManager.getInstance();
     Connection conn = dataSource.getConnection();
     PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
    hintManager.setDatabaseShardingValue(3);
    try (ResultSet rs = preparedStatement.executeQuery()) {
        while (rs.next()) {
            // ...
        }
    }
}