Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id E34ACB5DBE9 for ; Wed, 29 Jun 2011 16:51:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 46440-01 for ; Wed, 29 Jun 2011 19:51:32 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) by mail.postgresql.org (Postfix) with ESMTP id 45723B5DBEF for ; Wed, 29 Jun 2011 16:51:32 -0300 (ADT) Received: from mail06.corp.redhat.com (zmail06.collab.prod.int.phx2.redhat.com [10.5.5.45]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p5TJpTIh019846; Wed, 29 Jun 2011 15:51:29 -0400 Date: Wed, 29 Jun 2011 15:51:29 -0400 (EDT) From: Justin Bertram To: Heikki Linnakangas Cc: pgsql-jdbc@postgresql.org Message-ID: <147817721.851047.1309377089469.JavaMail.root@zmail06.collab.prod.int.phx2.redhat.com> In-Reply-To: <4E0AEFDB.30701@enterprisedb.com> Subject: Re: Possible oversight in org.postgresql.xa.PGXAConnection.commitPrepared(Xid xid) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.5.5.71] X-Mailer: Zimbra 6.0.9_GA_2686 (ZimbraWebClient - SAF3 (Linux)/6.0.9_GA_2686) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/78 X-Sequence-Number: 22002 I had to adjust the patch so that the new variable xaerrcode was passed int= o the PGXAException constructor (on line 522 of the patched code) rather th= an XAException.XAER_RMERR. Once that was done recovery worked just as expe= cted. Thanks for your work on this! Justin ----- Original Message ----- From: "Heikki Linnakangas" To: "Justin Bertram" Cc: pgsql-jdbc@postgresql.org Sent: Wednesday, June 29, 2011 4:26:51 AM Subject: Re: [JDBC] Possible oversight in org.postgresql.xa.PGXAConnection.= commitPrepared(Xid xid) On 29.06.2011 01:52, Justin Bertram wrote: > I'm bringing this back up because, while the XAER_RMERR works in most cas= es it fails in at least one. > > Consider the scenario where the database is shutdown administratively dur= ing org.postgresql.xa.PGXAConnection.commitPrepared(Xid xid). The driver w= ill throw an XAException with an errorCode of XAER_RMERR back to the transa= ction manager. However, the pg_prepared_xacts table will still contain a r= ow for the transaction. > > The method org.postgresql.xa.PGXAConnection.commitPrepared(Xid xid) is in= voked by the transaction manager as part of its call to javax.transaction.x= a.XAResource.commit(..) [1]. This is the JTA mapping of the xa_commit() fu= nction from the XA specification [2]. According to this document, a return= of XAER_RMERR means: > > An error occurred in committing the work performed on behalf of the tr= ansaction > branch and the branch=E2=80=99s work has been rolled back. Note that r= eturning this error > signals a catastrophic event to a transaction manager since other reso= urce > managers may successfully commit their work on behalf of this branch. = This error > should be returned only when a resource manager concludes that it can = never > commit the branch and that it cannot hold the branch=E2=80=99s resourc= es in a prepared > state. Otherwise, [XA_RETRY] should be returned. > > However, since the pg_prepared_xacts table still contains a row for the t= ransaction the XAER_RMERR is not accurate. A "catastrophic" failure has no= t occurred. It would be possible for the transaction manager to recover th= is transaction once the database is available again if XA_RETRY was returne= d instead. > > I think it would be better if commitPrepared could differentiate between = errors and return either XAER_RMERR or XA_RETRY as appropriate. Otherwise = just about any failure during commitPrepared will result in unrecoverable t= ransactions and require manual intervention to clean up the pg_prepared_xac= ts table. Yes, good catch, we should be more careful to use the right error code.=20 For starters we could detect errors caused by failed connection, which=20 would include the case of server shutdown, and throw XA_RETRY. But I=20 guess we should be conservative here, and return XA_RETRY for anything=20 else than cases where we know for sure that the prepared transaction is=20 not there anymore. If COMMIT PREPARED is called for a non-existing=20 global transaction id, you get the error "prepared transaction with=20 identifier \"%s\" does not exist", with sqlstate 42704=20 (ERRCODE_UNDEFINED_OBJECT). We use ERRCODE_UNDEFINED_OBJECT for a lot of=20 things in the backend, but when you're issuing COMMIT PREPARED, I think=20 it's reasonable to assume that if you get that error code, the prepared=20 transaction is missing. Attached patch implements that. I don't have a transaction manager=20 environment for this at hand right now, so this is completely untested. Do you have a setup ready where you could test this? --=20 Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com