Permalink
Browse files

feat: include root cause exception in case transaction fails (#628)

In case transaction fails (i.e. 'current transaction is aborted,
commands ignored until end of transaction block'), the initial
error is included to the exception chain, so it is easier to
analyze.
  • Loading branch information...
vlsi committed Sep 18, 2016
1 parent 154c463 commit 51775c167a0037a812b4cd4c22126e5ae853c8aa
@@ -102,6 +102,11 @@
*/
private String lastSetSearchPathQuery;
/**
* The exception that caused the last transaction to fail.
*/
private SQLException transactionFailCause;
public QueryExecutorImpl(PGStream pgStream, String user, String database,
int cancelSignalTimeout, Properties info, Logger logger) throws SQLException, IOException {
super(logger, pgStream, user, database, cancelSignalTimeout, info);
@@ -2450,7 +2455,13 @@ private SQLException receiveErrorResponse() throws IOException {
logger.debug(" <=BE ErrorMessage(" + errorMsg.toString() + ")");
}
return new PSQLException(errorMsg);
PSQLException error = new PSQLException(errorMsg);
if (transactionFailCause == null) {
transactionFailCause = error;
} else {
error.initCause(transactionFailCause);
}
return error;
}
private SQLWarning receiveNoticeResponse() throws IOException {
@@ -2524,9 +2535,11 @@ private void receiveRFQ() throws IOException {
// Update connection state.
switch (tStatus) {
case 'I':
transactionFailCause = null;
setTransactionState(TransactionState.IDLE);
break;
case 'T':
transactionFailCause = null;
setTransactionState(TransactionState.OPEN);
break;
case 'E':
@@ -145,10 +145,12 @@ public void handleError(SQLException newError) {
queryString = queries[resultIndex].toString(parameterLists[resultIndex]);
}
super.handleError(new BatchUpdateException(
GT.tr("Batch entry {0} {1} was aborted: {2} Call getNextException to see the cause.",
BatchUpdateException batchException = new BatchUpdateException(
GT.tr("Batch entry {0} {1} was aborted: {2} Call getNextException to see other errors in the batch.",
resultIndex, queryString, newError.getMessage()),
newError.getSQLState(), uncompressUpdateCount()));
newError.getSQLState(), uncompressUpdateCount());
batchException.initCause(newError);
super.handleError(batchException);
}
resultIndex++;
@@ -166,6 +168,7 @@ public void handleCompletion() throws SQLException {
batchException.getSQLState(),
uncompressUpdateCount()
);
newException.initCause(batchException.getCause());
SQLException next = batchException.getNextException();
if (next != null) {
newException.setNextException(next);
@@ -15,16 +15,15 @@
private ServerErrorMessage _serverError;
public PSQLException(String msg, PSQLState state, Throwable cause) {
super(msg, state == null ? null : state.getState());
initCause(cause);
super(msg, state == null ? null : state.getState(), cause);
}
public PSQLException(String msg, PSQLState state) {
this(msg, state, null);
super(msg, state == null ? null : state.getState());
}
public PSQLException(ServerErrorMessage serverError) {
this(serverError.toString(), new PSQLState(serverError.getSQLState()));
super(serverError.toString(), serverError.getSQLState());
_serverError = serverError;
}

0 comments on commit 51775c1

Please sign in to comment.