Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: map integrity constraint violation to XA_RBINTEGRITY instead of XAER_RMFAIL #1175

Merged
merged 6 commits into from Jul 14, 2018
@@ -328,7 +328,7 @@ public int prepare(Xid xid) throws XAException {

return XA_OK;
} catch (SQLException ex) {
throw new PGXAException(GT.tr("Error preparing transaction. prepare xid={0}", xid), ex, XAException.XAER_RMERR);
throw new PGXAException(GT.tr("Error preparing transaction. prepare xid={0}", xid), ex, mapSQLStateToXAErrorCode(ex));
}
}

@@ -495,7 +495,7 @@ private void commitOnePhase(Xid xid) throws XAException {
conn.commit();
conn.setAutoCommit(localAutoCommitMode);
} catch (SQLException ex) {
throw new PGXAException(GT.tr("Error during one-phase commit. commit xid={0}", xid), ex, XAException.XAER_RMFAIL);
throw new PGXAException(GT.tr("Error during one-phase commit. commit xid={0}", xid), ex, mapSQLStateToXAErrorCode(ex));
}
}

@@ -584,6 +584,18 @@ public boolean setTransactionTimeout(int seconds) {
return false;
}

private int mapSQLStateToXAErrorCode(SQLException sqlException) {
if (isPostgreSQLIntegrityConstraintViolation(sqlException)) {

This comment has been minimized.

Copy link
@vlsi

vlsi May 7, 2018

Member

Is there any reason why integrity constraint violation exceptions only are treated special?

This comment has been minimized.

Copy link
@janvdbergh

janvdbergh Jun 12, 2018

Author Contributor

These exceptions can be mapped to the correct XA Error Code. When Postgres reported an integrity constraint violation that means the change has been rolled back and can be reported as such. We can not make the same claim for other exceptions.

return XAException.XA_RBROLLBACK;

This comment has been minimized.

Copy link
@vlsi

vlsi Jul 14, 2018

Member

@janvdbergh , what do you think of using XA_RBINTEGRITY instead?

A condition that violates the integrity of the resource was detected

https://docs.oracle.com/javaee/6/api/javax/transaction/xa/XAException.html#XA_RBINTEGRITY

}

return XAException.XAER_RMFAIL;
}

private boolean isPostgreSQLIntegrityConstraintViolation(SQLException sqlException) {
return sqlException instanceof PSQLException && sqlException.getSQLState().matches("23\\d{3}");

This comment has been minimized.

Copy link
@vlsi

vlsi May 7, 2018

Member

While regexp looks clever, it is actually specified to mean that? What if the code clashes somehow?

This comment has been minimized.

Copy link
@janvdbergh

janvdbergh Jun 12, 2018

Author Contributor

Correct. Replaced it with a non-regex version.

}

private enum State {
/**
* {@code PGXAConnection} not associated with a XA-transaction. You can still call {@link #getConnection()} and
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.