Skip site navigation (1) Skip section navigation (2)

Psycopg2 and LIXA

From: Christian Ferrari <camauz(at)yahoo(dot)com>
To: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Psycopg2 and LIXA
Date: 2012-02-11 17:37:46
Message-ID: 1328981866.35435.YahooMailNeo@web29502.mail.ird.yahoo.com (view raw or flat)
Thread:
Lists: psycopg
Dear All,
I'm the founder of LIXA project ( http://sourceforge.net/projects/lixa/ http://lixa.sourceforge.net/manuals/ ) and I'm interested in integrating LIXA with Psycopg2.
LIXA is LIbre XA, a free and open source XA compliant transaction manager: it implements XA interface to deal with the Resouce Managers and implements TX interface to deal with the Application Programs.
LIXA supports PostgreSQL as a Resource Manager and allows to create distributed transaction (2 phase commit) with PostgreSQL, MySQL, Oracle and DB2.
XA and TX are interfaces designed for C (and C++) programs.
I am now interested in expanding the LIXA project scope to Python language and Python programmers.
It seems to me Psycopg2 is a good start point because it wraps the PostgreSQL C API; LIXA too wraps libpq-fe.h

It's time to describe my issue to ask some help to you.
First of all, I'm not a Python expert.
I've generated a LIXA wrapper using SWIG and it works fine, the usage is something like this (my development environment is based on Ubuntu 10.04):

tiian(at)mojan:~/src/swig$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lixa
>>> lixa.tx_open()
0
>>> lixa.tx_begin()
0
>>> lixa.tx_commit()
0
>>> lixa.tx_close()
0
>>> 


tx_open(), tx_begin(), tx_commit(), tx_close() are the standard functions described in "TX (Transaction Demarcation) Specification" (CAE X/Open standard).
The operations related to the Resource Managers must be put between "tx_begin()" and "tx_commit()".

TX standards specifies the Resource Managers must be opened by the Transaction Manager using "tx_open()" by the Application Program. This is the key point: Psycopg2 supplies its own method to get a valid PostgreSQL session: "psycopg2.connect(...)", while LIXA wants to create it using "lixa.tx_open()": the Transaction Manager must open all the Resource Managers, not only PostgreSQL.
Using C as the development language, LIXA supplies some convenience methods:
PGconn *lixa_pq_get_conn_by_rmid(int rmid);
PGconn *lixa_pq_get_conn_by_pos(int pos);
PGconn *lixa_pq_get_conn(void);

if there is only one PostgreSQL database, the third one is good enought. Using the convenience method, the C program becomes:

[...]

    /* open the resource manager(s) */
    if (TX_OK != (txrc = tx_open())) {
        fprintf(stderr, "tx_open error: %d\n", txrc);
        exit(txrc);
    }
    /* retrieve PostgreSQL connection */
    conn = lixa_pq_get_conn();
    /* start a new transaction */
    if (TX_OK != (txrc = tx_begin())) {
        fprintf(stderr, "tx_begin error: %d\n", txrc);
        exit(txrc);
    }

[...]

the Application Program does not use "PQconectdb(...)": the Transaction Manager calls it inside "tx_open()" and the Application Program retrieves the connection using a convenience method (lixa_pq_get_conn).
The same when the connection must be closed using "tx_close()".


Translated to Python, it becomes:

tiian(at)mojan:~/src/swig$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lixa
>>> lixa.tx_open()
0
>>> conn = lixa.lixa_pq_get_conn()
>>> print conn
<Swig Object of type 'PGconn *' at 0xb77e9638>
 
Coming back to Psycopg2, 
psycopg2.connect(...)
does not return a simple handler to a PostgreSQL connection, but a complex object with additional methods and properties.

Thinking about integration between LIXA and Psycopg2 I'm proposing three different paths:
1. patching Psycopg2 substituting "PQconnectdb(...)" with "lixa_pq_get_conn": I don't like this solution because the patch must be managed and tested every time Psycopg2 release something new; I don't think it would be well accepted by the users too
2. using "psycopg2.extensions - Extensions to the DB API" I have found at this paragraph:
http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.connection
it could be viable path, but I'm not able to figure out it nowadays
3. it could be very easy to overload the "psycopg2.connect()" method: if it accepted a "PGconn *" too, the integration would be straightforward like: psycopg2.connect([...], lixa.lixa_pq_get_conn() )

What's your opinions and suggestions?

Thanks in advance for your help.
Regards, 
Ch.F.


-------------------------------------------------------------------
Decent workarounds outperform poor solutions

Responses

psycopg by date

Next:From: Daniele VarrazzoDate: 2012-02-12 02:51:57
Subject: Re: Psycopg2 and LIXA
Previous:From: Vojtěch RylkoDate: 2012-01-30 17:30:58
Subject: Re: Named cursor problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group