Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 7026B2E0E1B for ; Tue, 13 Nov 2007 03:30:23 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 34001-04 for ; Tue, 13 Nov 2007 03:30:17 -0400 (AST) Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.184]) by postgresql.org (Postfix) with ESMTP id 1C3DE2E0CD9 for ; Tue, 13 Nov 2007 03:30:15 -0400 (AST) Received: by mu-out-0910.google.com with SMTP id i10so1967154mue for ; Mon, 12 Nov 2007 23:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:subject:from:to:cc:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer; bh=CuqUrrNYl0rstxxXuNMeTS5CXanjW3UmEk58UcR7BV4=; b=A9GT1QNNu6m6/NDZSaQ7Oz1CxPYJCOkn/L/oyqS+bQBogOx5ZjYJkQB60zMqVataNWQ0Q+wuYvx17x2Vh1hMwBfu0fEbPxFd9FK2kdgd0RHrwP5eSIzaWP3s+c9peM9uOz0lVLfQJ0Ol7d4CT1rjjsD9P3blSekq1geJVg/dDdw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:subject:from:to:cc:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer; b=qvR6KLONErwNLOvutzjHPeByMIJFgoOds+kJGdH+hAnnHJCgJCkGG+arOYCT1EvALZ4549Rs/btOLrGgkyIwiFg/qYNN0ZBO/+rGU6ipv/oPIStIRDM5kHTp0xFo7RADUBI3Yod8TocNP4gvwmDHulArRcuqZKDFxjTC0a+XeOg= Received: by 10.86.77.5 with SMTP id z5mr5500714fga.1194939014279; Mon, 12 Nov 2007 23:30:14 -0800 (PST) Received: from ?10.192.57.81? ( [193.134.216.2]) by mx.google.com with ESMTPS id w7sm12439353mue.2007.11.12.23.30.11 (version=SSLv3 cipher=RC4-MD5); Mon, 12 Nov 2007 23:30:12 -0800 (PST) Subject: Re: XAResource implementation From: =?ISO-8859-1?Q?jo=EBl?= Winteregg To: Heikki Linnakangas Cc: pgsql-jdbc@postgresql.org, Ludovic Orban 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> Content-Type: multipart/mixed; boundary="=-CoPc+hd5iT8RGHwljF09" Date: Tue, 13 Nov 2007 08:30:10 +0100 Message-Id: <1194939010.5505.24.camel@hatman> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/51 X-Sequence-Number: 17806 --=-CoPc+hd5iT8RGHwljF09 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Hello Heikki, Many thanks for your answer and for the time you took to do some testing ! As you can see I put Ludovic Orban as Cc because he is the BTM developer and he seems to be interested to this issue too... > 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. > Ah Yes, you're right, your transaction just looks perfect ! > 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 : SELECT gid FROM pg_prepared_xacts > LOG: execute S_1: BEGIN > LOG: execute : insert into messages(content) values ($1) > DETAIL: parameters: $1 = 'hello, world!' > LOG: execute S_2: COMMIT > LOG: execute S_1: BEGIN > LOG: execute : select content from messages > LOG: execute : select content from messages > LOG: execute S_2: COMMIT On my side, I tryed the following example: http://docs.codehaus.org/download/attachments/9240687/HibernateBTM.zip?version=2 (see attachement to see my modifications) and my pg logs look like this for the following code: userTransaction.begin(); loopCountQuery(sf); //many select count queries userTransaction.commit(); 2007-11-13 08:13:51 CET DETAIL: parameters: $1 = 'username 6' 2007-11-13 08:13:51 CET LOG: execute S_2: COMMIT 2007-11-13 08:13:51 CET LOG: execute S_1: BEGIN 2007-11-13 08:13:51 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:13:51 CET DETAIL: parameters: $1 = 'username 7' 2007-11-13 08:13:51 CET LOG: execute S_2: COMMIT 2007-11-13 08:13:51 CET LOG: execute S_1: BEGIN 2007-11-13 08:13:51 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:13:51 CET DETAIL: parameters: $1 = 'username 8' 2007-11-13 08:13:51 CET LOG: execute S_2: COMMIT 2007-11-13 08:13:51 CET LOG: execute S_1: BEGIN 2007-11-13 08:13:51 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:13:51 CET DETAIL: parameters: $1 = 'username 9' 2007-11-13 08:13:51 CET LOG: execute S_2: COMMIT 2007-11-13 08:13:51 CET LOG: execute S_1: BEGIN 2007-11-13 08:13:51 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:13:51 CET DETAIL: parameters: $1 = 'username 10' 2007-11-13 08:13:51 CET LOG: execute S_2: COMMIT When I use the pure JDBC driver (not the XA implementation), I don't have this behaviour (BEGIN and COMMIT are related to userTransaction from application level) and not anymore around each query: 2007-11-13 08:19:55 CET LOG: execute S_1: BEGIN 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 0' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 1' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 2' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 3' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 4' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 5' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 6' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 7' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 8' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 9' 2007-11-13 08:19:55 CET LOG: execute : select count(*) as y0_ from USERS this_ where this_.NAME=$1 2007-11-13 08:19:55 CET DETAIL: parameters: $1 = 'username 10' 2007-11-13 08:19:55 CET LOG: execute S_2: COMMIT As I'm not a JTA and JDBC expert at all, I'm not able to provide you more information, I'm sorry about that... I hope this behaviour is not coming from my miss-understanding ! Thanks for your help ! Regards, Joël On Mon, 2007-11-12 at 09:59 +0000, Heikki Linnakangas wrote: > 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 : SELECT gid FROM pg_prepared_xacts > LOG: execute S_1: BEGIN > LOG: execute : insert into messages(content) values ($1) > DETAIL: parameters: $1 = 'hello, world!' > LOG: execute S_2: COMMIT > LOG: execute S_1: BEGIN > LOG: execute : select content from messages > LOG: execute : select content from messages > LOG: execute S_2: COMMIT > --=-CoPc+hd5iT8RGHwljF09 Content-Disposition: attachment; filename=Test.java Content-Type: text/x-java; name=Test.java; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 7bit package bitronix.examples.hibernate; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.transaction.UserTransaction; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import bitronix.examples.hibernate.entities.User; import bitronix.tm.TransactionManagerServices; import bitronix.tm.resource.jdbc.PoolingDataSource; public class Test { private static UserTransaction userTransaction; private static int nbTuples = 10; public static void main(String[] args) throws Exception { initDataSources(); initTransactionManager(); SessionFactory sf = new Configuration().configure("/hibernate1.cfg.xml").buildSessionFactory(); userTransaction.begin(); populateDB(sf); userTransaction.commit(); userTransaction.begin(); loopCountQuery(sf); userTransaction.commit(); sf.close(); System.out.println("END bru"); } private static void populateDB(SessionFactory sf) { Session session = sf.getCurrentSession(); /* Add nbTuples users*/ for(int i = 0; i <= nbTuples; i++){ User u = new User(); u.setName("username "+i); session.save(u); } } private static void loopCountQuery(SessionFactory sf){ Session session = sf.getCurrentSession(); for(int i = 0; i <= nbTuples; i++){ double nb = Double.valueOf(String.valueOf((Integer) session.createCriteria(User.class) .add(Restrictions.eq("name", "username "+i)) .setProjection(Projections.rowCount()) .uniqueResult())); System.out.println("User: usernaem "+i+" = "+nb); } } /** * Initialize the transaction manager. */ private static void initTransactionManager() { userTransaction = TransactionManagerServices.getTransactionManager(); } /** * Create the datasource and it into the embedded JNDI server. */ private static void initDataSources() throws NamingException { Context ctx = new InitialContext(); ctx.createSubcontext("jdbc"); PoolingDataSource dataSource1 = getDataSource_db(); ctx.rebind("jdbc/testDS1", dataSource1); } /** * Create the first datasource. */ private static PoolingDataSource getDataSource_db() { /* Uncomment this to have the Last Resource Commit implementation */ // PoolingDataSource ds = new PoolingDataSource(); // ds.setUniqueName("jdbc/testDS1"); // ds.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource"); // ds.setMaxPoolSize(3); // ds.setAllowLocalTransactions(true); // ds.getDriverProperties().setProperty("driverClassName", "org.postgresql.Driver"); // ds.getDriverProperties().setProperty("url", "jdbc:postgresql://127.0.0.1:5432/test"); // ds.getDriverProperties().put("user", "hibernate"); // ds.getDriverProperties().put("password", "hibernate"); // // ds.init(); // return ds; PoolingDataSource ds = new PoolingDataSource(); ds.setUniqueName("jdbc/testDS1"); ds.setClassName("org.postgresql.xa.PGXADataSource"); ds.setMaxPoolSize(3); ds.getDriverProperties().put("databaseName", "test"); ds.getDriverProperties().put("user", "hibernate"); ds.getDriverProperties().put("password", "hibernate"); ds.getDriverProperties().put("serverName", "127.0.0.1"); ds.getDriverProperties().put("portNumber", "5432"); ds.init(); return ds; } } --=-CoPc+hd5iT8RGHwljF09 Content-Disposition: attachment; filename=table.sql Content-Type: text/x-sql; name=table.sql; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 7bit CREATE TABLE users ( id serial NOT NULL, name varchar(50) ); --=-CoPc+hd5iT8RGHwljF09--