The TMSTransaction component is designed to manage distributed transactions. Distributed transactions can be performed to one or more connections connected to the same or to different databases or servers. Within each connection a separate branch of the transaction is performed. TMSTransaction is based on the Microsoft Distributed Transaction Coordinator (MSDTC) functionality. Transactions can be managed by StartTransaction, Commit, and Rollback methods of TMSTransaction. For more information on distributed transactions and MSDTC please refer to MSDN.
TMSTransaction does not support local transactions.To control local transactions you should use methods of the TMSConnection component.
The example below demonstrates using distributed transaction coordinated by Microsoft Distributed Transaction Coordinator:
begin
MSConnection1.Connect;
MSConnection2.Connect;
MSTransaction.AddConnection(MSConnection1);
MSTransaction.AddConnection(MSConnection2);
MSTransaction.StartTransaction;
MSSQL1.Connection := MSConnection1;
MSSQL2.Connection := MSConnection2;
try
MSSQL1.Execute;
MSSQL2.Execute;
MSTransaction.Commit;
except
MSTransaction.Rollback;
end;
end;
After both connections are established, they are added to the list of connections managed by MSTransaction. Call to StartTransaction makes both TMSConnections components work in the same distributed transaction. After MSSQL1 and MSSQL2 are executed, MSTransaction.Commit ensures that all changes to both databases are committed. If an exception occurs during execution, MSTransaction.Rollback restores both databases to their initial state.