From: joël Winteregg <joel.winteregg@gmail.com>
To: Heikki Linnakangas <heikki@enterprisedb.com>
Cc: pgsql-jdbc@postgresql.org
Cc: Ludovic Orban <ludovic.orban@gmail.com>
Subject: Re: XAResource implementation
Date: Tue, 13 Nov 2007 08:30:10 +0100
Message-ID: <1194939010.5505.24.camel@hatman> (raw)
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>
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 <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
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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <unnamed>: 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 <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
>
Attachments:Test.java (text/x-java, 3.7K) download
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;
}
}
table.sql (text/x-sql, 64B) download
CREATE TABLE users
(
id serial NOT NULL,
name varchar(50)
);
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: joel.winteregg@gmail.com, heikki@enterprisedb.com, ludovic.orban@gmail.com
Subject: Re: XAResource implementation
In-Reply-To: <1194939010.5505.24.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> <47382418.604@enterprisedb.com> <1194939010.5505.24.camel@hatman>
* 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