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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/xa/PGXAConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

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

private int mapSQLStateToXAErrorCode(SQLException sqlException) {
if (isPostgreSQLIntegrityConstraintViolation(sqlException)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Member

@vlsi vlsi Jul 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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().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
Expand Down
28 changes: 26 additions & 2 deletions pgjdbc/src/test/java/org/postgresql/test/xa/XADataSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +64,8 @@ public void setUp() throws Exception {
st.close();

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

clearAllPrepared();

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

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

Expand Down Expand Up @@ -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
*/
@Test
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 {
xaRes.prepare(xid);

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);
Expand Down