From: Heikki Linnakangas <heikki@enterprisedb.com>
To: ludovic orban <ludovic.orban@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: XA end then join fix for WebLogic
Date: Tue, 07 Nov 2006 12:31:53 +0000
Message-ID: <45507CB9.2020206@enterprisedb.com> (raw)
In-Reply-To: <c016d00b0611010407vfa765bfx53da8c128e0e1108@mail.gmail.com>
References: <c016d00b0610310758s3dadc02w954de80c678be462@mail.gmail.com>
<4547C128.40404@enterprisedb.com>
<c016d00b0611010202m3438da13t4035164c822f1b78@mail.gmail.com>
<45487C70.6000604@enterprisedb.com>
<c016d00b0611010407vfa765bfx53da8c128e0e1108@mail.gmail.com>
Hi Ludovic,
I finally put aside some time to test this..
ludovic orban wrote:
> Hi again,
>
>> Can you elaborate how 'on statement creation' is generally more
>> performant? It seems to me that it leads to more start/end calls. At
>> least in a straightforward implementation of start(TMJOIN), that means
>> more round-trips to the database. Have you seen or done any performance
>> tests on this?
>
> It is more performant because it allows greater concurrency for a
> fixed amount of connections in the pool. This of course requires that
> the DB fully supports transaction interleaving or else the wrapper has
> to use a mix of both techniques and you loose that advantage. I
> measured an average 25% speed improvement with FirebirdSQL on a highly
> concurrent test (250+ XA transactions per second).
Well, if the server has the resources to do more concurrent work than
the pool size allows, just increase the pool. If it doesn't, then it
doesn't no matter what you do.
Did you try increasing the pool size instead of interleaving normal work
and commits?
>> This is an extreme example, but the same scenario is possible with a
>> larger connection pool, just harder to trigger.
>
> Thread A won't block since commit() does not require a connection, it
> just needs to use the reference it kept on the enlisted XAResource to
> be able to execute the 2PC protocol. Refer to the JTA spec chapter
> 3.4.6, Resource Sharing for details.
I guess it depends on the RM, but let's assume a typical client/server
scenario where one XAConnection has one TCP connection to the server.
The commit() will certainly have to submit the commit message to the
server somehow, and unless it uses carrier pigeons or some other
out-of-band channel, it has to use the TCP connection it has. If that
connection is busy, waiting for another query to finish, the commit will
have to wait.
In fact, because the commit() call will have to wait for the current
statement to finish, that might lead to a longer response time.
Especially if you have a mix of long and short transactions.
I just tried this with Firebird (1.5.3) and JayBird (2.1.0) that you did
your performance test with. It *does* seem to suffer from the scenario I
described. See attached test case.
>> Passing a Connection-object in a bean invocation just isn't sane, even
>> if it was allowed by the spec. What if you pass a ResultSet-object,
>> should it work in BeanB? What about updates to an updatable ResultSet?
>
> I agree this isn't proper design but as you can read from the comments
> that's not the point. There are other ways to get the same kind of
> unexpected bahavior when playing with suspend/resume. Consider this
> example:
>
> tm.begin();
>
> c = getXADataSource().getConnection();
> c.createStatement().executeUpdate("UPDATE A");
>
> tx = tm.suspend();
>
> tm.begin();
> c.createStatement().executeUpdate("UPDATE B");
> tm.rollback();
>
> tm.resume(tx);
>
> c.close();
>
> tm.commit();
>
> What do you think should happen to UPDATE A and UPDATE B ? What do you
> think will happen with 'on statement creation' enlistment and with 'on
> connection acquirement' ? In my opinion, update A should be committed
> and update B rolled back. What will actually happen depends on both
> the resource and the enlistment policy implementations.
Agreed, A should be committed and B rolled back.
> With 'on statement creation' what I expect will happen: update A
> committed and update B rolled back while with 'on connection
> acquirement' both will get committed because update B is executed in a
> local transaction context since it has not been enlisted in the
> sub-transaction's context.
Umm, sub-transaction?
> While I totally agree that those examples are corner-case and not good
> examples of how things should be written, those are nevertheless traps
> that application programmers can easily fall on during the
> implementation process. I guess you can imagine how insane it is when
> you have to troubleshoot some code where you ask the TM to rollback
> and you actually see the update committed to the DB.
I can feel your pain :). Some years ago, we found out the hard way that
a FixPack to WebSphere Application Server 3.5.X (can't remember exactly)
introduced a bug that made it ignore setRollbackOnly-calls if an EJB
throwed an exception. That lead to corruption in our production
database, and the customer was not happy.
--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.comAttachments:XATest.java (text/x-java, 3.8K) download
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Random;
import javax.sql.XAConnection;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.firebirdsql.pool.FBConnectionPoolDataSource;
/**
* Prior to running the test, adjust the hard-coded connection parameters, and create test table:
*
* CREATE TABLE foo (bar INTEGER);
*
* Expected output if it the driver could issue an out-of-band commit:
*
* Initialized
* Doing first update
* Doing second update
* Trying to commit
* Commit finished
* Second update committed
*
* Instead, it will hang at "Trying to commit" line until DEADLOCK_TIMEOUT is reached (default
* 10 seconds).
*/
public class XATest {
public static void main(String[] args) throws Exception {
Xid initxid = new CustomXid(1);
FBConnectionPoolDataSource ds = new FBConnectionPoolDataSource();
ds.setDatabase("/tmp/foo.fdb");
ds.setUserName("SYSDBA");
ds.setPassword("sysdba");
XAConnection xaconn = ds.getXAConnection();
XAResource xares = xaconn.getXAResource();
// Initialize table
xares.start(initxid, XAResource.TMNOFLAGS);
Connection conn = xaconn.getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM foo");
stmt.executeUpdate("INSERT INTO foo VALUES (1234)");
xares.end(initxid, XAResource.TMSUCCESS);
xares.commit(initxid, true);
System.out.println("Initialized");
// Start updating transaction that is ended but not committed
Xid neverfinishxid = new CustomXid(2);
xares.start(neverfinishxid, XAResource.TMNOFLAGS);
System.out.println("Doing first update");
conn.createStatement().executeUpdate("UPDATE foo SET bar = 5678");
xares.end(neverfinishxid, XAResource.TMSUCCESS);
// Launch a committer thread that will try to commit neverfinishxid after a while
Committer committer = new Committer();
committer.xares = xares;
committer.commitThis = neverfinishxid;
new Thread(committer).start();
// Start another update that blocks because it waits for neverfinishxid
Xid blockxid = new CustomXid(3);
xares.start(blockxid, XAResource.TMNOFLAGS);
System.out.println("Doing second update");
conn.createStatement().executeUpdate("UPDATE foo SET bar = 0000");
System.out.println("Second update committed");
conn.commit();
}
public static class Committer implements Runnable
{
Xid commitThis;
XAResource xares;
public void run()
{
try {
Thread.sleep(2000);
System.out.println("Trying to commit");
xares.commit(commitThis, true);
System.out.println("Commit finished");
} catch(Exception ex)
{
ex.printStackTrace();
}
}
}
static class CustomXid implements Xid {
private static Random rand = new Random(System.currentTimeMillis());
byte[] gtrid = new byte[Xid.MAXGTRIDSIZE];
byte[] bqual = new byte[Xid.MAXBQUALSIZE];
CustomXid(int i)
{
rand.nextBytes(gtrid);
gtrid[0] = (byte)i;
gtrid[1] = (byte)i;
gtrid[2] = (byte)i;
gtrid[3] = (byte)i;
gtrid[4] = (byte)i;
bqual[0] = 4;
bqual[1] = 5;
bqual[2] = 6;
}
public int getFormatId() {
return 0;
}
public byte[] getGlobalTransactionId() {
return gtrid;
}
public byte[] getBranchQualifier() {
return bqual;
}
public boolean equals(Object o) {
Xid other = (Xid)o;
if (other.getFormatId() != this.getFormatId())
return false;
if (!Arrays.equals(other.getBranchQualifier(), this.getBranchQualifier()))
return false;
if (!Arrays.equals(other.getGlobalTransactionId(), this.getGlobalTransactionId()))
return false;
return true;
}
}
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: heikki@enterprisedb.com, ludovic.orban@gmail.com
Subject: Re: XA end then join fix for WebLogic
In-Reply-To: <45507CB9.2020206@enterprisedb.com>
References: <c016d00b0610310758s3dadc02w954de80c678be462@mail.gmail.com> <4547C128.40404@enterprisedb.com> <c016d00b0611010202m3438da13t4035164c822f1b78@mail.gmail.com> <45487C70.6000604@enterprisedb.com> <c016d00b0611010407vfa765bfx53da8c128e0e1108@mail.gmail.com> <45507CB9.2020206@enterprisedb.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox