diff --git a/src/main/java/com/jd/jdbc/queryservice/NativeQueryService.java b/src/main/java/com/jd/jdbc/queryservice/NativeQueryService.java index f3ddb85..6e34cfd 100644 --- a/src/main/java/com/jd/jdbc/queryservice/NativeQueryService.java +++ b/src/main/java/com/jd/jdbc/queryservice/NativeQueryService.java @@ -98,7 +98,7 @@ public Query.CommitResponse commit(final IContext context, final Query.Target ta return Query.CommitResponse.newBuilder().build(); } catch (SQLException e) { this.errorCount(); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildCommitExceptionReason(e), e); } finally { this.endSummary(); } @@ -112,7 +112,7 @@ public Query.RollbackResponse rollback(final IContext context, final Query.Targe return Query.RollbackResponse.newBuilder().build(); } catch (SQLException e) { this.errorCount(); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildRollbackExceptionReason(e), e); } finally { this.endSummary(); } @@ -139,7 +139,7 @@ public VtResultSet execute(final IContext context, final Query.Target target, fi } catch (SQLException e) { this.errorCount(); context.cancel(e.getMessage()); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildExceptionReason(e), e); } finally { this.endSummary(); } @@ -153,7 +153,7 @@ public VtResultSet execute(final IContext context, final Query.Target target, fi } catch (SQLException e) { this.errorCount(); context.cancel(e.getMessage()); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildTransactionExceptionReason(e), e); } finally { if (conn != null) { conn.unlock(false); @@ -177,13 +177,13 @@ public StreamIterator streamExecute(final IContext context, final Query.Target t } InnerConnection connection = statefulConnectionPool.getNoStatefulConn(); - ResultSet resultSet = null; + ResultSet resultSet; try { resultSet = connection.streamExecute(sql); } catch (SQLException e) { connection.close(); context.cancel(e.getMessage()); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildExceptionReason(e), e); } return new StreamIterator(connection, resultSet); } @@ -229,7 +229,7 @@ public BatchVtResultSet executeBatch(final IContext context, final Query.Target } catch (SQLException e) { this.errorCount(); context.cancel(e.getMessage()); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildExceptionReason(e), e); } finally { this.endSummary(); } @@ -250,7 +250,7 @@ public BatchVtResultSet executeBatch(final IContext context, final Query.Target } catch (SQLException e) { this.errorCount(); context.cancel(e.getMessage()); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildTransactionExceptionReason(e), e); } finally { if (conn != null) { conn.unlock(false); @@ -282,13 +282,12 @@ public BeginBatchVtResultSet beginExecuteBatch(final IContext ctx, final Query.T StatefulConnection conn = null; try { conn = statefulConnectionPool.newConn(options.getWorkloadValue() != Query.ExecuteOptions.Workload.DBA_VALUE); - List allVtResultSets = new ArrayList<>(); List sqlList = getMultiSql(ctx, queries); conn.setAutoCommitFalse(); ExecuteResult result = conn.execute(BEGIN + sqlList.get(0)); boolean isQuery = result.getStatement().getMoreResults(); List vtResultSets = toVtResultSets(isQuery, result.getStatement()); - allVtResultSets.addAll(vtResultSets); + List allVtResultSets = new ArrayList<>(vtResultSets); for (int i = 1; i < sqlList.size(); i++) { ExecuteResult curResult = conn.execute(sqlList.get(i)); @@ -297,12 +296,11 @@ public BeginBatchVtResultSet beginExecuteBatch(final IContext ctx, final Query.T allVtResultSets.addAll(curVtResultSets); } return new BeginBatchVtResultSet(this.tablet.getAlias(), conn.getConnID(), allVtResultSets); - } catch (SQLException e) { this.errorCount(); rollbackAndRelease(conn); ctx.cancel(e.getMessage()); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildTransactionExceptionReason(e), e); } finally { if (conn != null) { conn.unlock(false); @@ -347,7 +345,7 @@ public BeginVtResultSet beginExecute(final IContext context, final Query.Target } catch (SQLException e) { context.cancel(e.getMessage()); rollbackAndRelease(conn); - throw SQLExceptionTranslator.translate(getReason(e), e); + throw SQLExceptionTranslator.translate(buildTransactionExceptionReason(e), e); } finally { if (conn != null) { conn.unlock(false); @@ -364,7 +362,6 @@ public Query.ReleaseResponse release(final IContext context, final Query.Target } catch (SQLException e) { logger.warn(e.getMessage(), e); } - return Query.ReleaseResponse.newBuilder().build(); } @@ -410,8 +407,20 @@ private void rollbackAndRelease(final StatefulConnection conn) throws SQLExcepti } } - private String getReason(final Exception e) { - return e.getMessage() + "; tablet :" + TopoProto.tabletToHumanString(tablet); + private String buildExceptionReason(final Exception e) { + return e.getMessage() + " tablet :" + TopoProto.tabletToHumanString(tablet); + } + + private String buildTransactionExceptionReason(final Exception e) { + return e.getMessage() + " current SQL in transaction; tablet :" + TopoProto.tabletToHumanString(tablet); + } + + private String buildCommitExceptionReason(final Exception e) { + return e.getMessage() + " current SQL is commit; tablet :" + TopoProto.tabletToHumanString(tablet); + } + + private String buildRollbackExceptionReason(final Exception e) { + return e.getMessage() + " current SQL is rollback; tablet :" + TopoProto.tabletToHumanString(tablet); } private void startSummary() { diff --git a/src/main/java/com/jd/jdbc/vitess/VitessStatement.java b/src/main/java/com/jd/jdbc/vitess/VitessStatement.java index 5859435..f5c320a 100755 --- a/src/main/java/com/jd/jdbc/vitess/VitessStatement.java +++ b/src/main/java/com/jd/jdbc/vitess/VitessStatement.java @@ -533,7 +533,6 @@ private int[] executeBatchUsingMultiQueries(final IContext ctx, final boolean re } catch (SQLException e) { cleanResultSets(); this.lastInsertId = 0; - logger.error(e.getMessage(), e); throw e; } finally { clearBatchInternal(); diff --git a/src/test/java/testsuite/TestSuite.java b/src/test/java/testsuite/TestSuite.java index 70ad7f2..def0ec8 100644 --- a/src/test/java/testsuite/TestSuite.java +++ b/src/test/java/testsuite/TestSuite.java @@ -59,13 +59,23 @@ protected static Connection getConnection(TestSuiteEnv env) throws SQLException public static void closeConnection(Connection... conns) { for (Connection conn : conns) { - if (conn != null) { - try { - conn.close(); - } catch (SQLException throwables) { - printFail(throwables.getMessage()); - throw new RuntimeException(throwables); - } + closeConnection(conn); + } + } + + public static void closeConnection(List connectionList) { + for (Connection connection : connectionList) { + closeConnection(connection); + } + } + + public static void closeConnection(Connection conn) { + if (conn != null) { + try { + conn.close(); + } catch (SQLException throwables) { + printFail(throwables.getMessage()); + throw new RuntimeException(throwables); } } } @@ -85,23 +95,6 @@ public Thread newThread(final Runnable r) { return pool; } - public static void closeConnection(Connection conn) { - if (conn != null) { - try { - conn.close(); - } catch (SQLException throwables) { - printFail(throwables.getMessage()); - throw new RuntimeException(throwables); - } - } - } - - public static void closeConnection(List connectionList) { - for (Connection connection : connectionList) { - closeConnection(connection); - } - } - protected static List iterateExecFile(String filename, Class caseClazz) throws IOException { BufferedReader br = Files.newBufferedReader(Paths.get(filename), StandardCharsets.UTF_8); StringBuilder content = new StringBuilder();