use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.
the class DruidUpdateParser method statementParse.
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
//这里限制了update分片表的个数只能有一个
if (ctx.getTables() != null && getUpdateTableCount() > 1 && !schema.isNoSharding()) {
String msg = "multi table related update not supported,tables:" + ctx.getTables();
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
String tableName = StringUtil.removeBackquote(update.getTableName().getSimpleName().toUpperCase());
TableConfig tc = schema.getTables().get(tableName);
if (RouterUtil.isNoSharding(schema, tableName)) {
//整个schema都不分库或者该表不拆分
RouterUtil.routeForTableMeta(rrs, schema, tableName, rrs.getStatement());
rrs.setFinishedRoute(true);
return;
}
String partitionColumn = tc.getPartitionColumn();
String joinKey = tc.getJoinKey();
if (tc.isGlobalTable() || (partitionColumn == null && joinKey == null)) {
//修改全局表 update 受影响的行数
RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), rrs.getStatement(), tc.isGlobalTable());
rrs.setFinishedRoute(true);
return;
}
confirmShardColumnNotUpdated(update, schema, tableName, partitionColumn, joinKey, rrs);
if (schema.getTables().get(tableName).isGlobalTable() && ctx.getRouteCalculateUnit().getTablesAndConditions().size() > 1) {
throw new SQLNonTransientException("global table is not supported in multi table related update " + tableName);
}
}
use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.
the class RouterUtil method tryRouteForTables.
/**
* 多表路由
*/
public static RouteResultset tryRouteForTables(SchemaConfig schema, DruidShardingParseInfo ctx, RouteCalculateUnit routeUnit, RouteResultset rrs, boolean isSelect, LayerCachePool cachePool) throws SQLNonTransientException {
List<String> tables = ctx.getTables();
if (schema.isNoSharding() || (tables.size() >= 1 && isNoSharding(schema, tables.get(0)))) {
return routeToSingleNode(rrs, schema.getDataNode(), ctx.getSql());
}
//只有一个表的
if (tables.size() == 1) {
return RouterUtil.tryRouteForOneTable(schema, ctx, routeUnit, tables.get(0), rrs, isSelect, cachePool);
}
Set<String> retNodesSet = new HashSet<String>();
//每个表对应的路由映射
Map<String, Set<String>> tablesRouteMap = new HashMap<String, Set<String>>();
//分库解析信息不为空
Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions = routeUnit.getTablesAndConditions();
if (tablesAndConditions != null && tablesAndConditions.size() > 0) {
//为分库表找路由
RouterUtil.findRouteWithcConditionsForTables(schema, rrs, tablesAndConditions, tablesRouteMap, ctx.getSql(), cachePool, isSelect);
if (rrs.isFinishedRoute()) {
return rrs;
}
}
//为全局表和单库表找路由
for (String tableName : tables) {
TableConfig tableConfig = schema.getTables().get(tableName.toUpperCase());
if (tableConfig == null) {
String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
if (tableConfig.isGlobalTable()) {
//全局表
if (tablesRouteMap.get(tableName) == null) {
tablesRouteMap.put(tableName, new HashSet<String>());
}
tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
} else if (tablesRouteMap.get(tableName) == null) {
//余下的表都是单库表
tablesRouteMap.put(tableName, new HashSet<String>());
tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
}
}
boolean isFirstAdd = true;
for (Map.Entry<String, Set<String>> entry : tablesRouteMap.entrySet()) {
if (entry.getValue() == null || entry.getValue().size() == 0) {
throw new SQLNonTransientException("parent key can't find any valid datanode ");
} else {
if (isFirstAdd) {
retNodesSet.addAll(entry.getValue());
isFirstAdd = false;
} else {
retNodesSet.retainAll(entry.getValue());
if (retNodesSet.size() == 0) {
//两个表的路由无交集
String errMsg = "invalid route in sql, multi tables found but datanode has no intersection " + " sql:" + ctx.getSql();
LOGGER.warn(errMsg);
throw new SQLNonTransientException(errMsg);
}
}
}
}
if (retNodesSet != null && retNodesSet.size() > 0) {
String tableName = tables.get(0);
TableConfig tableConfig = schema.getTables().get(tableName.toUpperCase());
if (tableConfig.isDistTable()) {
routeToDistTableNode(tableName, schema, rrs, ctx.getSql(), tablesAndConditions, cachePool, isSelect);
return rrs;
}
if (retNodesSet.size() > 1 && isAllGlobalTable(ctx, schema)) {
// mulit routes ,not cache route result
if (isSelect) {
rrs.setCacheAble(false);
routeToSingleNode(rrs, retNodesSet.iterator().next(), ctx.getSql());
} else {
//delete 删除全局表的记录
routeToMultiNode(isSelect, rrs, retNodesSet, ctx.getSql(), true);
}
} else {
routeToMultiNode(isSelect, rrs, retNodesSet, ctx.getSql());
}
}
return rrs;
}
use of java.sql.SQLNonTransientException in project Mycat-Server by MyCATApache.
the class DruidMysqlRouteStrategyTest method testERroute.
public void testERroute() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
String sql = "insert into orders (id,name,customer_id) values(1,'testonly',1)";
RouteResultset rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals("dn1", rrs.getNodes()[0].getName());
sql = "insert into orders (id,name,customer_id) values(1,'testonly',2000001)";
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
Assert.assertEquals(false, rrs.isCacheAble());
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
// can't update join key
sql = "update orders set id=1 ,name='aaa' , customer_id=2000001";
String err = null;
try {
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
} catch (SQLNonTransientException e) {
err = e.getMessage();
}
Assert.assertEquals(true, err.startsWith("Parent relevant column can't be updated ORDERS->CUSTOMER_ID"));
// route by parent rule ,update sql
sql = "update orders set id=1 ,name='aaa' where customer_id=2000001";
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
// route by parent rule but can't find datanode
sql = "update orders set id=1 ,name='aaa' where customer_id=-1";
try {
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
} catch (Exception e) {
err = e.getMessage();
}
Assert.assertEquals(true, err.startsWith("can't find datanode for sharding column:"));
// route by parent rule ,select sql
sql = "select * from orders where customer_id=2000001";
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
Assert.assertEquals(true, rrs.isCacheAble());
Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
// route by parent rule ,delete sql
sql = "delete from orders where customer_id=2000001";
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
Assert.assertEquals("dn2", rrs.getNodes()[0].getName());
//test alias in column
sql = "select name as order_name from orders order by order_name limit 10,5";
rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
MySqlStatementParser parser = new MySqlStatementParser("SELECT name AS order_name FROM orders ORDER BY order_name LIMIT 0,15");
SQLStatement statement = parser.parseStatement();
// Assert.assertEquals(sql, rrs.getNodes()[0].getStatement());
}
use of java.sql.SQLNonTransientException in project jdk8u_jdk by JetBrains.
the class SQLNonTransientConnectionExceptionTests method test13.
/**
* Create SQLNonTransientConnectionException and validate it is an instance of
* SQLNonTransientException
*/
@Test
public void test13() {
Exception ex = new SQLNonTransientConnectionException();
assertTrue(ex instanceof SQLNonTransientException);
}
use of java.sql.SQLNonTransientException in project jdk8u_jdk by JetBrains.
the class SQLNonTransientExceptionTests method test7.
/**
* Create SQLNonTransientException with message, and Throwable
*/
@Test
public void test7() {
SQLNonTransientException ex = new SQLNonTransientException(reason, t);
assertTrue(ex.getMessage().equals(reason) && ex.getSQLState() == null && cause.equals(ex.getCause().toString()) && ex.getErrorCode() == 0);
}
Aggregations