use of com.dangdang.ddframe.rdb.sharding.api.HintManager in project sharding-jdbc by dangdangdotcom.
the class ShardingMasterSlaveForPStatementWithSelectTest method assertSelectForHintAndForceMaster.
@Test
public void assertSelectForHintAndForceMaster() throws SQLException, DatabaseUnitException {
HintManager hintManager = HintManager.getInstance();
hintManager.setMasterRouteOnly();
hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
hintManager.addTableShardingValue("t_order", "order_id", 1000);
String sql = "SELECT `t_order`.order_id, `t_order`.user_id, `t_order`.status FROM `t_order` WHERE `t_order`.`user_id` = ? AND `t_order`.`order_id` = ?";
assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleMasterTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 1000);
}
use of com.dangdang.ddframe.rdb.sharding.api.HintManager in project sharding-jdbc by dangdangdotcom.
the class DatabaseTest method assertSQL.
@Test
public void assertSQL() {
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setDatabaseShardingValue(1);
assertTarget("select * from tesT", "ds_1");
assertTarget("insert into test values (1,2)", "ds_1");
assertTarget("update test set a = 1", "ds_1");
assertTarget("delete from test where id = 2", "ds_1");
hintManager.setDatabaseShardingValue(2);
assertTarget("select * from tesT", "ds_0");
}
}
use of com.dangdang.ddframe.rdb.sharding.api.HintManager in project sharding-jdbc by dangdangdotcom.
the class Main method printHintSimpleSelect.
private static void printHintSimpleSelect(final DataSource dataSource) throws SQLException {
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id";
try (HintManager hintManager = HintManager.getInstance();
Connection conn = dataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
hintManager.addTableShardingValue("t_order", "order_id", 1001);
try (ResultSet rs = preparedStatement.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getInt(2));
System.out.println(rs.getInt(3));
}
}
}
}
Aggregations