Skip to content

Commit

Permalink
Optimizing the exception information for exceptions within a transact…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
wangweicugw committed Jul 28, 2023
1 parent 780938d commit 59b493d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
41 changes: 25 additions & 16 deletions src/main/java/com/jd/jdbc/queryservice/NativeQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down Expand Up @@ -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<VtResultSet> allVtResultSets = new ArrayList<>();
List<String> sqlList = getMultiSql(ctx, queries);
conn.setAutoCommitFalse();
ExecuteResult result = conn.execute(BEGIN + sqlList.get(0));
boolean isQuery = result.getStatement().getMoreResults();
List<VtResultSet> vtResultSets = toVtResultSets(isQuery, result.getStatement());
allVtResultSets.addAll(vtResultSets);
List<VtResultSet> allVtResultSets = new ArrayList<>(vtResultSets);

for (int i = 1; i < sqlList.size(); i++) {
ExecuteResult curResult = conn.execute(sqlList.get(i));
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/jd/jdbc/vitess/VitessStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
41 changes: 17 additions & 24 deletions src/test/java/testsuite/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Connection> 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);
}
}
}
Expand All @@ -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<Connection> connectionList) {
for (Connection connection : connectionList) {
closeConnection(connection);
}
}

protected static <T extends TestSuiteCase> List<T> iterateExecFile(String filename, Class<T> caseClazz) throws IOException {
BufferedReader br = Files.newBufferedReader(Paths.get(filename), StandardCharsets.UTF_8);
StringBuilder content = new StringBuilder();
Expand Down

0 comments on commit 59b493d

Please sign in to comment.