What is single and two-phase Commit? Diff Between Xa and non-xa datasource?
The difference is that one-phase commit is usually used within one system or database while two-phase commit is used for distributed transactions which span multiple DBs or systems. Let me show you simple example for each
One-phase commit
BEGIN
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );
COMMIT;
This is classic atomic transaction written in PL/SQL (for Java EE world, imagine EJB method which can be transactional too), there is just one phase where all actions are performed and either the commit or rollback is made.
Two-phase commit
//pseodocode
BEGIN
UPDATE db1; //updates DB on another machine
UPDATE someCloudStorage; //update something on the cloud
INSERT INTO SomeTable VALUES(...);
COMMIT;
Now you are dealing with different systems on different machines but you want to write successfully to all of them or fail(that's why it's distributed transaction). So here comes the Two-phase commit protocol. Basically transaction manager broadcasts "prepare for commit" message and than waits for OK response from each machine. If everybody says it's ok, everything gets committed - if not, all transactions are rolled back.
Note that if you want to use XA transactions in Java, you will need a JDBC driver with XA support (and properly configured XA datasource).
Originally Posted by Petr Mensik in StackOverflow
Comments
Post a Comment