Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5Kii-0000jL-BS for pgsql-jdbc@arkaria.postgresql.org; Tue, 12 Feb 2013 18:36:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U5Kih-0003BF-Qu for pgsql-jdbc@arkaria.postgresql.org; Tue, 12 Feb 2013 18:36:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5Kig-0003AP-Ds for pgsql-jdbc@postgresql.org; Tue, 12 Feb 2013 18:36:42 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U5Kic-0002eG-F3 for pgsql-jdbc@postgresql.org; Tue, 12 Feb 2013 18:36:42 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 530722861A; Tue, 12 Feb 2013 10:36:35 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 4D77540197; Tue, 12 Feb 2013 10:36:35 -0800 (PST) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id AA07FC15BC; Tue, 12 Feb 2013 10:36:34 -0800 (PST) X-Virus-Scanned: amavisd-new at zcs-prod-ext-mta-2.vmware.com Received: from zcs-prod-ext-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-ext-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id V8949LrhosoK; Tue, 12 Feb 2013 10:36:34 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 88207C1525; Tue, 12 Feb 2013 10:36:34 -0800 (PST) Received: from [192.168.1.90] (unknown [10.113.62.14]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA; Tue, 12 Feb 2013 10:36:33 -0800 (PST) Message-ID: <511A8BAC.7080400@vmware.com> Date: Tue, 12 Feb 2013 20:36:28 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: Bryan Varner CC: "pgsql-jdbc@postgresql.org" Subject: Re: PostgreSQL XAResource & GlassFish 3.1.2.2 References: <511A2C7F.1040104@vmware.com> <511A675B.3020207@polarislabs.com> In-Reply-To: <511A675B.3020207@polarislabs.com> Content-Type: multipart/mixed; boundary="------------070108010300090900000808" X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------070108010300090900000808 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------070108010300090900000808 Content-Type: text/x-diff; name="synchronize-pgxaconnection.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="synchronize-pgxaconnection.patch" 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)")); --------------070108010300090900000808 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc --------------070108010300090900000808--