PHP and two phase commit transactions
| From: | Christian Ferrari | Date: | Tue, 29 May 2012 20:27:53 +0000 |
| Subject: | PHP and two phase commit transactions | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-60688@lists.php.net to get a copy of this message | ||
Dear all,
I'm the author of LIXA project (http://lixa.sourceforge.net/) and I would discuss a little
about "two phase commit" transactions for PHP applications.
"Two
phase commit" means I can write a program that uses two (or more)
Resource Managers (for example databases, like MySQL and PostgreSQL)
with ACID transactions that span all the databases. With two phase
commit I can perform a *global* commit that commits the changes inside
two or more databases at the *same* *time*. This is quite different than
performing two (or more) distinct commits: with distinct commits it may
happen the first one completes and the second one catches an error..
LIXA
project implements two X/Open standards: "XA specification" and "TX
(transaction demarcation) specification". XA describes an interface
between the Resource Managers (databases) and the Transaction Manager
(for example LIXA); TX describes an interface between the Application
Program (your own custom software) and the Transaction Manager (for
example LIXA).
The "TX (transaction demarcation) specification"
is a C API very easy to understand and use when you are developing a C
(or C++) software.
When I started developing LIXA, I figured out
it would have been straightforward to wrap it for other languages like
PHP, Perl, Python, Ruby and so on...
Unfortunately I discovered
it's not easy to extend the TX API to other languages: creating a
wrapper for TX API is trivial because it's just the matter of some SWIG (http://www.swig.org/)
hacking.
Integrating the TX wrapped interface with *the* *environment* is a completely different task.
These are the issues I partially solved integrating LIXA with PHP.
1.
XA interface is a C level interface: the PHP database drivers must be
changed at C source level to avoid many leaps between C and PHP code.
The most important PHP database drivers are distributed with PHP core
itself => patching their source code is equivalent to patch PHP core.
2.
As a consequence of the first point, the LIXA PHP wrapper itself should
be distributed with PHP core to avoid dependencies between some core
extensions and an external extension.
3. The TX API does not deal
with the concept of "connection" because it's implicit: every
process/thread has its own connection with every Resource Manager
(database). Better explained: tx_open(), tx_begin(), tx_commit(),
tx_rollback(), tx_close() (and so on...) do not specify the handlers
necessary to distinguish connection objects.
4. The TX API design does not take into account the concept of "persistent connections"
and "connection pooling".
I
afforded issues 1 and 2 creating a global experimental patch for PHP
core itself (you can find it in "ext/php" directory of LIXA tarball or
directly at SVN repository. http://lixa.svn.sourceforge.net/viewvc/lixa/ext/php/).
Issue
number 3 was bypassed and, for the sake of trivial "hello world
equivalent examples", the integration seems to work properly.
Issue 4
is quite difficult to address: I ignored "persistent connection"
disabling LIXA feature when the application asks a persistent
connection, but I was not able to deal with Oracle "connection pools" of oci8
driver.
It's time to made a balance of LIXA and PHP integration: I
"successfully" integrated mysqli (with MySQL supplied library) and pgsql
drivers. There are some examples and 7 case tests available.
I was *not* able to figure out a smart patch for "oci8" driver and I stopped my effort.
To
conclude, I think 98% of the use cases does *not* need two phase
commit, but if you are developing the other 2% of the use cases -
without two phase commit - you probably could create some clumsy
"solution" to avoid the "two phase commit issue".
What do you
think about PHP and two phase commit? Do you think the game isn't worth
the candle? Do you think there's room for two phase commit inside the
PHP userland?
In the next future I will concentrate my LIXA efforts
in expanding the number of different Resource Managers for C
programmers because "patching" PHP for LIXA integration is a too hard
task for me myself. Is there someone interested in picking-up LIXA PHP
experimental extension and going further with the task? Is there someone
interested in "two phase commit" and "distributed transaction
processing" in general?
Every comment/hint/suggestion would be appreciated.
Regards
Ch.F.
-------------------------------------------------------------------
Decent workarounds outperform poor solutions