Skip to content
Permalink
Browse files

Fix bug when call XAResource.start with TMJOIN flag, the old localAut…

…oCommitMode lost.
  • Loading branch information
chunlinyao committed Nov 27, 2015
1 parent e0a9b41 commit df09e2bee35e49238d883cc6881deb5d8dea6401
Showing with 38 additions and 8 deletions.
  1. +27 −1 org/postgresql/test/xa/XADataSourceTest.java
  2. +11 −7 org/postgresql/xa/PGXAConnection.java
@@ -340,7 +340,33 @@ public void testRestoreOfAutoCommit() throws Exception {
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.commit(xid, true);

assertTrue(!conn.getAutoCommit());
assertFalse("XaResource should have restored connection autocommit mode after commit or rollback to the initial state.", conn.getAutoCommit());

// Test true case
conn.setAutoCommit(true);

xid = new CustomXid(15);
xaRes.start(xid, XAResource.TMNOFLAGS);
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.commit(xid, true);

assertTrue("XaResource should have restored connection autocommit mode after commit or rollback to the initial state.", conn.getAutoCommit());

}

public void testRestoreOfAutoCommitEndThenJoin() throws Exception {
// Test with TMJOIN
conn.setAutoCommit(true);

Xid xid = new CustomXid(16);
xaRes.start(xid, XAResource.TMNOFLAGS);
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.start(xid, XAResource.TMJOIN);
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.commit(xid, true);

assertTrue("XaResource should have restored connection autocommit mode after start(TMNOFLAGS) end() start(TMJOIN) and then commit or rollback to the initial state.", conn.getAutoCommit());

}

/**
@@ -233,14 +233,18 @@ public void start(Xid xid, int flags) throws XAException {
} else if(state == STATE_ENDED)
throw new PGXAException(GT.tr("Transaction interleaving not implemented"), XAException.XAER_RMERR);

try
{
localAutoCommitMode = conn.getAutoCommit();
conn.setAutoCommit(false);
}
catch (SQLException ex)
// Only need save localAutoCommitMode for NOFLAGS, TMRESUME and TMJOIN already saved old localAutoCommitMode.
if (flags == TMNOFLAGS)
{
throw new PGXAException(GT.tr("Error disabling autocommit"), ex, XAException.XAER_RMERR);
try
{
localAutoCommitMode = conn.getAutoCommit();
conn.setAutoCommit(false);
}
catch (SQLException ex)
{
throw new PGXAException(GT.tr("Error disabling autocommit"), ex, XAException.XAER_RMERR);
}
}

// Preconditions are met, Associate connection with the transaction

0 comments on commit df09e2b

Please sign in to comment.
You can’t perform that action at this time.