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 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("Error preparing transaction. prepare xid={0}", xid), ex, XAException.XAER_RMERR);
throw new PGXAException("Error preparing transaction. prepare xid={0}", xid), ex, mapSQLStateToXAErrorCode(ex));

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

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

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

This comment has been minimized.

Copy link

vlsi May 7, 2018


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

This comment has been minimized.

Copy link

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 Jul 14, 2018


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

A condition that violates the integrity of the resource was detected


return XAException.XAER_RMFAIL;

private boolean isPostgreSQLIntegrityConstraintViolation(SQLException sqlException) {
return sqlException instanceof PSQLException
&& sqlException.getSQLState().length() == 5
&& sqlException.getSQLState().substring(0, 2).equals("23");

private enum State {
* {@code PGXAConnection} not associated with a XA-transaction. You can still call {@link #getConnection()} and
@@ -23,13 +23,11 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.Arrays;
import java.util.Random;

import javax.sql.XAConnection;
import javax.sql.XADataSource;

import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
@@ -66,6 +64,8 @@ public void setUp() throws Exception {

TestUtil.createTable(_conn, "testxa1", "foo int");
TestUtil.createTable(_conn, "testxa2", "foo int primary key");
TestUtil.createTable(_conn, "testxa3", "foo int references testxa2(foo) deferrable");


@@ -92,6 +92,8 @@ public void tearDown() throws SQLException {

TestUtil.dropTable(_conn, "testxa3");
TestUtil.dropTable(_conn, "testxa2");
TestUtil.dropTable(_conn, "testxa1");

@@ -780,6 +782,28 @@ public void testNetworkIssueOnRollback() throws Exception {

* When using deferred constraints a contraint violation can occur on prepare. This has to be
* mapped to the correct XA Error Code
public void testMappingOfConstraintViolations() throws Exception {
Xid xid = new CustomXid(1);
xaRes.start(xid, XAResource.TMNOFLAGS);
assertEquals(0, conn.createStatement().executeUpdate("SET CONSTRAINTS ALL DEFERRED"));
assertEquals(1, conn.createStatement().executeUpdate("INSERT INTO testxa3 VALUES (4)"));
xaRes.end(xid, XAResource.TMSUCCESS);

try {

fail("Prepare is expected to fail as an integrity violation occurred");
} catch (XAException xae) {
assertEquals("Prepare call with deferred constraints violations expects XA_RBROLLBACK",
XAException.XA_RBROLLBACK, xae.errorCode);

* We don't support transaction interleaving. public void testInterleaving1() throws Exception {
* Xid xid1 = new CustomXid(1); Xid xid2 = new CustomXid(2);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.