From: Heikki Linnakangas <heikki@enterprisedb.com>
To: joël Winteregg <joel.winteregg@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: XAResource implementation
Date: Mon, 12 Nov 2007 09:59:52 +0000
Message-ID: <47382418.604@enterprisedb.com> (raw)
In-Reply-To: <1194819933.5545.100.camel@hatman>
References: <1193736934.5565.40.camel@hatman>
<4727420A.4060209@enterprisedb.com>
<1193757567.5565.46.camel@hatman>
<47275A50.2050201@enterprisedb.com>
<47275BA6.3030200@enterprisedb.com>
<1194819933.5545.100.camel@hatman>
joël Winteregg wrote:
> I said that BTM TransactionManager was "mostly" working because when I
> was using the XA implementation, SQL BEGIN and COMMIT where generated
> arround each SQL query (even if several queries were located inside the
> same transaction). So at the end, I used the TransactionManager with the
> "Last Resource Commit Optimization" which allow the use of a pure jdbc
> driver during transaction (I only have a single DB (1 Phase Commit) so
> it is 100% safe to use this specificity). In this case, my BEGIN and
> COMMIT statement where just perfect:
> BEGIN
> SELECT ..
> SELECT ..
> SELECT ..
> COMMIT
>
> So I was just wondering if my XA problem (BEGIN and COMMIT location when
> using XA) could come from the postgresql XA driver ?
Hmm. I downloaded the BTM newUserDemo.zip and modified it to run with
Postgres, and to run two queries in same transaction. Works for me.
Attached is the modified Test.java I used. To run:
0. Download newUserDemo.zip from
http://docs.codehaus.org/display/BTM/NewUserGuide
1. Put postgresql.jar in newUserDemo/lib
2. Copy the attached Test.java to newUserDemo/src/jtatest
3. Modify database/username/password in Test.java if necessary
4. Run the CREATE TABLE from derby-create.sql
Here's what I get in the Postgres log, with log_statements='all':
LOG: execute <unnamed>: SELECT gid FROM pg_prepared_xacts
LOG: execute S_1: BEGIN
LOG: execute <unnamed>: insert into messages(content) values ($1)
DETAIL: parameters: $1 = 'hello, world!'
LOG: execute S_2: COMMIT
LOG: execute S_1: BEGIN
LOG: execute <unnamed>: select content from messages
LOG: execute <unnamed>: select content from messages
LOG: execute S_2: COMMIT
--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.comAttachments:Test.java (text/x-java, 2.1K) download
package jtatest;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;
import javax.transaction.UserTransaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class Test {
public static void main(String[] args) throws Exception {
String who = "world";
if (args.length > 0)
who = args[0];
PoolingDataSource derby1Ds = new PoolingDataSource();
derby1Ds.setClassName("org.postgresql.xa.PGXADataSource");
derby1Ds.setUniqueName("derby1");
derby1Ds.setPoolSize(3);
derby1Ds.getDriverProperties().setProperty("user", "postgres");
derby1Ds.getDriverProperties().setProperty("databaseName", "postgres");
derby1Ds.init();
UserTransaction ut = TransactionManagerServices.getTransactionManager();
ut.begin();
try {
Connection c = derby1Ds.getConnection();
PreparedStatement stmt = c.prepareStatement("insert into messages(content) values (?)");
stmt.setString(1, "hello, " + who + "!");
stmt.executeUpdate();
stmt.close();
c.close();
ut.commit();
} catch (SQLException ex) {
ex.printStackTrace();
ut.rollback();
}
ut.begin();
try {
Connection c = derby1Ds.getConnection();
PreparedStatement stmt = c.prepareStatement("select content from messages");
ResultSet rs = stmt.executeQuery();
while(rs.next())
System.out.println(rs.getString(1));
rs.close();
stmt.close();
/* Run another query */
stmt = c.prepareStatement("select content from messages");
rs = stmt.executeQuery();
while(rs.next())
System.out.println(rs.getString(1));
rs.close();
stmt.close();
c.close();
ut.commit();
} catch (SQLException ex) {
ex.printStackTrace();
ut.rollback();
}
derby1Ds.close();
}
}
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, joel.winteregg@gmail.com
Subject: Re: XAResource implementation
In-Reply-To: <47382418.604@enterprisedb.com>
References: <1193736934.5565.40.camel@hatman> <4727420A.4060209@enterprisedb.com> <1193757567.5565.46.camel@hatman> <47275A50.2050201@enterprisedb.com> <47275BA6.3030200@enterprisedb.com> <1194819933.5545.100.camel@hatman> <47382418.604@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