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. Apache ShardingSphere can also do mandatory routing by adding comments to SQL.
Main application scenarios for Hint:
Hint algorithms require users to implement the interface of org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm
.
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:
algorithmClassName: xxx.xxx.xxx.HintXXXAlgorithm
tableStrategy:
hint:
algorithmClassName: xxx.xxx.xxx.HintXXXAlgorithm
defaultTableStrategy:
none:
defaultKeyGenerateStrategy:
type: SNOWFLAKE
column: order_id
props:
sql-show: true
HintManager hintManager = HintManager.getInstance();
hintManager.addDatabaseShardingValue
to add sharding key value of data source.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.
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
.
// 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()) {
// ...
}
}
}
For the SQL Hint function, the comment format only supports /* */
for now. The content needs to start with SHARDINGSPHERE_HINT:
, and optional attributes include:
{table}.SHARDING_DATABASE_VALUE
: used to add the data source sharding value corresponding to {table}
table, multiple attributes are separated by commas;{table}.SHARDING_TABLE_VALUE
: used to add the table sharding value corresponding to {table}
table, multiple attributes are separated by commas.Users can use
SHARDING_DATABASE_VALUE
to set sharding value in hint route to some certain sharding database without sharding tables.
/* SHARDINGSPHERE_HINT: t_order.SHARDING_DATABASE_VALUE=1, t_order.SHARDING_TABLE_VALUE=1 */
SELECT * FROM t_order;