Daniel Magin’s Weblog

My life in the Developers World

How to create a two phase commit with Delphi and InterBase

This week i was given a 2h webinar over Understanding Transactions with InterBase and Delphi ( a playback you can find here LINK ).

One open Question was how to create a two phase commit with IBX in Delphi.

Follow these steps and you have created your first two phase commit application:

  1. Create a new Delphi Project
  2. Place for (minimum) 2 InterBases Databases a TIBDataBase Component
  3. Edit the connection String from the TIBDataBase Components to connect diffrent databases
    - for example
    - database1 ->127.0.0.1:c:\data\employee_1.ib
    - database2 ->127.0.0.1:c:\data\employee_2.ib
  4. Place 5 Buttons with following Captions
    - add database
    - start transaction
    - run queries
    - commit transaction
    - rollback tranaction
  5. Add ONE TIBTransaction component
  6. Place two TIBQuery Components (you can use later also TIBDataSet for showing data in a gird or what ever)
  7. edit the SQL Property of each TIBQueryComponent
    - update table1 set Field1=’Ernie’ where ID=1
    - update table2 set Field1=’Bert’ where ID2
    table1 is in this example a table in DataBase1 and
    table2 is in this example a table in DataBase2
  8. set of IBQuery1 properties:
    - DataBase -> IBDataBase1
    - Transaction -> IBTransaction1
  9. set of IBQuery2 properties:
    - DataBase -> IBDataBase2
    - Transaction -> IBTransaction1
  10. insert this code in the button with the caption “Add DataBase”
     IBTransaction1.AddDatabase(IBDatabase1);
     IBTransaction1.AddDatabase(IBDatabase2);
  11. insert this code in the button with the caption “start transaction”
     IBTransaction1.StartTransaction;
  12. insert this code in the button with the caption “run queries”
    IBQuery1.ExecSQL;
    IBQuery2.ExecSQL;
  13. insert this code in the button with the caption “commit transaction”
    IBTransaction1.Commit;
  14. insert this code in the button with the caption “rollback transaction”
    IBTransaction1.Rollback;
  15. in the form event on create open both databases and also on close do not forget to close the databses
  16. Compile
  17. Run and have fun

end;

 

that’s all folks

 

regards daniel

April 25, 2008 Posted by dmagin | Embarcadero (CodeGear) stuff | | 1 Comment