Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id A5B5E9FB647 for ; Sun, 17 Jun 2007 04:41:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 44010-01 for ; Sun, 17 Jun 2007 04:40:58 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.191]) by postgresql.org (Postfix) with ESMTP id B921A9FB624 for ; Sun, 17 Jun 2007 04:41:00 -0300 (ADT) Received: by mu-out-0910.google.com with SMTP id i10so1779063mue for ; Sun, 17 Jun 2007 00:40:59 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:sender; b=ImuqCZzQMwJ1PbdonqlQtQJErSoU09G1uRvEtgH34gUbDvQxKMoDtzjqjQfJApDQBR8D11jHZrkBWcxRKOd99jYc87y6T7yI1j3BRkyxJNcVha7i2vmHLZ8edYS1xoh0JsfQ5ImECnTY+80jWoAaQ8tPjtElF/Wm7sCHHdef3sE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:sender; b=FWjVU4hA7n5xO9tRO6uDiL1h8QJ79hrrz3mS1qAmu4dvM4sARL0pObQnHmpHp+EpCn/w06gmdLWLI2t6LOzodAruyzmBPFFQpu14ZE/saYg6u7wRdzwbTWGgVt/ebf1vckBvYGpmLENZYgaCktR1G9SDTqChIz6xCJjOg6/xt70= Received: by 10.82.112.16 with SMTP id k16mr8892070buc.1182066059217; Sun, 17 Jun 2007 00:40:59 -0700 (PDT) Received: from ?192.168.0.199? ( [80.222.48.97]) by mx.google.com with ESMTP id d23sm4008186nfh.2007.06.17.00.40.56 (version=SSLv3 cipher=RC4-MD5); Sun, 17 Jun 2007 00:40:57 -0700 (PDT) Message-ID: <4674E56C.8060802@enterprisedb.com> Date: Sun, 17 Jun 2007 08:40:28 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: Luca Ferrari CC: pgsql-jdbc@postgresql.org Subject: Re: a simple example of XA (not working) References: <200706161607.52540.fluca1978@infinito.it> In-Reply-To: <200706161607.52540.fluca1978@infinito.it> Content-Type: multipart/mixed; boundary="------------090805030303050807080906" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/51 X-Sequence-Number: 17113 This is a multi-part message in MIME format. --------------090805030303050807080906 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Luca Ferrari wrote: > Hi all, > I'm trying to build a simple "manual" example of the use of the XA extension, > the following is the code: > > PGXADataSource dataSource = new PGXADataSource(); > dataSource.setDatabaseName("hrpm"); > dataSource.setUser("luca"); > dataSource.setServerName("localhost"); > > XAConnection connection = dataSource.getXAConnection(); > System.out.println("Connesso!"); > XAResource resource = connection.getXAResource(); > MyXid identifier = new MyXid(); > identifier.setBranchQualifier(new byte[] {0x01, 0x02, 0x03, 0x04, 0x05}); > identifier.setGlobalTransactionId(new byte[] {0x05, 0x04, 0x03, 0x02, 0x01}); > identifier.setFormatId(100); > > try{ > resource.start(identifier, XAResource.TMNOFLAGS); > > > Connection jdbcConnection = connection.getConnection(); > Statement statement = jdbcConnection.createStatement(); > String sql = "INSERT INTO ..."; > int inserted = statement.executeUpdate(sql); > > resource.end(identifier, XAResource.TMSUCCESS); > > int commit = resource.prepare(identifier); > if( commit == XAResource.XA_OK ) > resource.commit(identifier, false); > else > resource.rollback(identifier); > > > now the exception that is raised when the program commits the transaction is > the following: > > javax.transaction.xa.XAException: org.postgresql.util.PSQLException: ERROR: > prepared transaction with identifier "100_BQQDAgE=_AQIDBAU=" does not exist > at org.postgresql.xa.PGXAConnection.commitPrepared(PGXAConnection.java:412) > at org.postgresql.xa.PGXAConnection.commit(PGXAConnection.java:339) > > and the log of the database says: > > 2007-06-16 16:05:48 CEST LOG: execute : INSERT INTO .... > 2007-06-16 16:05:48 CEST LOG: execute : PREPARE > TRANSACTION '100_BQQDAgE=_AQIDBAU=' > 2007-06-16 16:05:48 CEST WARNING: non c'è nessuna transazione in corso > 2007-06-16 16:05:48 CEST LOG: execute : COMMIT > PREPARED '100_BQQDAgE=_AQIDBAU=' > 2007-06-16 16:05:48 CEST ERROR: prepared transaction with > identifier "100_BQQDAgE=_AQIDBAU=" does not exist > 2007-06-16 16:05:48 CEST STATEMENT: COMMIT PREPARED '100_BQQDAgE=_AQIDBAU=' > 2007-06-16 16:05:49 CEST LOG: unexpected EOF on client connection > > that is an error since it seems no one transaction has been started. However, > debugging the application, I've seen that the driver disables the autocommit > mode, thus where is the problem here? > I'm sorry, I'm not a XA expert, just learning. This seems to be an undesired side-effect of this patch that was committed in December: > date: 2006/12/01 10:13:46; author: jurka; state: Exp; lines: +19 -22 > A XAConnections default autocommit state should be true. The > driver was previously setting autocommit to false and assuming it > would stay that way. This caused two problems. First, some > applications expected to be able to issue local autocommit > transactions prior to begin(). Second, some TMs (Geronimo) set the > autocommit state to true themselves, which causes problems as the > driver did not expect it to be changed. This patch correctly disables > and enables autocommit around begin, prepare, commit, and rollback > methods. getConnection sets autocommit to false, so even though start set it to true, it's reset to false in the call to getConnection. Attached patch fixes that by explicitly setting autocommit to the right mode in PGXAConnection.getConnection. As a work-around, just call jdbcConnection.setAutoCommit(false) in your program before executing any queries. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------090805030303050807080906 Content-Type: text/x-diff; name="xa-autocommit-fix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="xa-autocommit-fix.patch" Index: org/postgresql/xa/PGXAConnection.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/xa/PGXAConnection.java,v retrieving revision 1.10 diff -c -r1.10 PGXAConnection.java *** org/postgresql/xa/PGXAConnection.java 1 Dec 2006 11:53:02 -0000 1.10 --- org/postgresql/xa/PGXAConnection.java 17 Jun 2007 07:40:03 -0000 *************** *** 61,67 **** public Connection getConnection() throws SQLException { ! return super.getConnection(); } public XAResource getXAResource() { --- 61,80 ---- public Connection getConnection() throws SQLException { ! Connection conn = super.getConnection(); ! ! // When we're outside an XA transaction, autocommit ! // is supposed to be true, per usual JDBC convention. ! // When an XA transaction is in progress, it should be ! // false. ! ! // super.getConnection resets autocommit to true, so we ! // have to set it to false before handing the connection ! // to the caller, if an XA transaction is active. ! if(state == STATE_ACTIVE) ! conn.setAutoCommit(false); ! ! return conn; } public XAResource getXAResource() { --------------090805030303050807080906--