pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Bryan Varner <bvarner@polarislabs.com>
Cc: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: PostgreSQL XAResource & GlassFish 3.1.2.2
Date: Tue, 12 Feb 2013 20:36:28 +0200
Message-ID: <511A8BAC.7080400@vmware.com> (raw)
In-Reply-To: <511A675B.3020207@polarislabs.com>
References: <A2B35D2698B0C74F9B8333525E67ED0135F89007@plindyexch02.polarislabs.lan>
	<511A2C7F.1040104@vmware.com>
	<511A675B.3020207@polarislabs.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>

On 12.02.2013 18:01, Bryan Varner wrote:
> What should be valid (and is not considered interleaving), is:
>
> Thread A Thread B
> -------- ---------
> xares.start(1234, TMNOFLAGS);
> doStuff();
> xares.end(1234);
> xares.start(1234, TMJOIN);
> doStuff();
> xares.end(1234);
> xares.start(1234, TMJOIN);
> doStuff();
> xares.end(1234);
>
> So long as the TM is serializing execution of A and B and not allowing
> branch interleaving.
>
> In this case, the XAResource is preforming work on behalf of more than
> one thread, but in the same XID context. The problem I think I'm seeing
> at this point (still trying to coordinate with the glassfish devs) is
> that the XAResource isn't fully completing execution of end() prior to
> the other thread invoking start(), even though the method invocation
> appears to be happening 'in order'. This would manifest as a classic
> race condition, and would not constitute transaction interleaving, since
> the XID in use is the same TX branch.

I see. I think there's yet another potential explanation: even if 
glassfish is careful to invoke the end() only after start() has fully 
finished in the other thread, the java memory model does not guarantee 
that the effects of the end() in one thread are visible to the other 
thread doing the start(). The update of the PGXAConnection's 
state-variable might be sitting in the CPU cache of the CPU running the 
first thread, not yet visible to the second thread. However, I'm not 
sure if glassfish could guarantee that the start() has really finished 
before end() is called, without using some operation that would also 
force the CPU cache to be flushed, making the effects visible.

In any case, it seems like we should add "synchronized" to all the 
public methods in PGXAConnection. The performance penalty should be 
minimal, and it would fix any race conditions of that sort.

Can you test the attached patch?

- Heikki

-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Attachments:

synchronize-pgxaconnection.patch (text/x-diff, 4.9K) download diff --git a/org/postgresql/xa/PGXAConnection.java b/org/postgresql/xa/PGXAConnection.java index c70d033..b320985 100644 --- a/org/postgresql/xa/PGXAConnection.java +++ b/org/postgresql/xa/PGXAConnection.java @@ -95,7 +95,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, /**** XAConnection interface ****/ - public Connection getConnection() throws SQLException + public synchronized Connection getConnection() throws SQLException { if (logger.logDebug()) debug("PGXAConnection.getConnection called"); @@ -137,23 +137,34 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - if (state != STATE_IDLE) + String methodName = method.getName(); + if (methodName.equals("commit") || + methodName.equals("rollback") || + methodName.equals("setSavePoint") || + (methodName.equals("setAutoCommit") && ((Boolean) args[0]).booleanValue())) { - String methodName = method.getName(); - if (methodName.equals("commit") || - methodName.equals("rollback") || - methodName.equals("setSavePoint") || - (methodName.equals("setAutoCommit") && ((Boolean) args[0]).booleanValue())) - { - throw new PSQLException(GT.tr("Transaction control methods setAutoCommit(true), commit, rollback and setSavePoint not allowed while an XA transaction is active."), - PSQLState.OBJECT_NOT_IN_STATE); - } - } - try { - return method.invoke(con, args); - } catch (InvocationTargetException ex) { - throw ex.getTargetException(); + synchronized(PGXAConnection.this) + { + if (state != STATE_IDLE) + { + throw new PSQLException(GT.tr("Transaction control methods setAutoCommit(true), commit, rollback and setSavePoint not allowed while an XA transaction is active."), + PSQLState.OBJECT_NOT_IN_STATE); + } + try { + return method.invoke(con, args); + } catch (InvocationTargetException ex) { + throw ex.getTargetException(); + } + } } + else + { + try { + return method.invoke(con, args); + } catch (InvocationTargetException ex) { + throw ex.getTargetException(); + } + } } } @@ -177,7 +188,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, * Postconditions: * 1. Connection is associated with the transaction */ - public void start(Xid xid, int flags) throws XAException { + public synchronized void start(Xid xid, int flags) throws XAException { if (logger.logDebug()) debug("starting transaction xid = " + xid); @@ -235,7 +246,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, * Postconditions: * 1. connection is disassociated from the transaction. */ - public void end(Xid xid, int flags) throws XAException { + public synchronized void end(Xid xid, int flags) throws XAException { if (logger.logDebug()) debug("ending transaction xid = " + xid); @@ -272,7 +283,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, * Postconditions: * 1. Transaction is prepared */ - public int prepare(Xid xid) throws XAException { + public synchronized int prepare(Xid xid) throws XAException { if (logger.logDebug()) debug("preparing transaction xid = " + xid); @@ -322,7 +333,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, * Postconditions: * 1. list of prepared xids is returned */ - public Xid[] recover(int flag) throws XAException { + public synchronized Xid[] recover(int flag) throws XAException { // Check preconditions if (flag != TMSTARTRSCAN && flag != TMENDRSCAN && flag != TMNOFLAGS && flag != (TMSTARTRSCAN | TMENDRSCAN)) throw new PGXAException(GT.tr("Invalid flag"), XAException.XAER_INVAL); @@ -380,7 +391,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, * Postconditions: * 1. Transaction is rolled back and disassociated from connection */ - public void rollback(Xid xid) throws XAException { + public synchronized void rollback(Xid xid) throws XAException { if (logger.logDebug()) debug("rolling back xid = " + xid); @@ -417,7 +428,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection, } } - public void commit(Xid xid, boolean onePhase) throws XAException { + public synchronized void commit(Xid xid, boolean onePhase) throws XAException { if (logger.logDebug()) debug("committing xid = " + xid + (onePhase ? " (one phase) " : " (two phase)"));

view thread (24+ messages)  latest in thread

Message-ID: <511A8BAC.7080400@vmware.com>
Permalink:  ../511A8BAC.7080400@vmware.com/
Also on:    postgresql.org/message-id/511A8BAC.7080400@vmware.com

 ·  · 

reply

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: hlinnakangas@vmware.com, bvarner@polarislabs.com
  Subject: Re: PostgreSQL XAResource & GlassFish 3.1.2.2
  In-Reply-To: <511A8BAC.7080400@vmware.com>
  References: <A2B35D2698B0C74F9B8333525E67ED0135F89007@plindyexch02.polarislabs.lan> <511A2C7F.1040104@vmware.com> <511A675B.3020207@polarislabs.com> <511A8BAC.7080400@vmware.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