Distributed transactions in managed mode
IBM MQ .NET classes use System.Transactions namespace for the distributed transactions support in managed mode. In the managed mode, MS DTC coordinates and manages distributed transactions across all the servers enlisted in a transaction.
IBM MQ .NET classes provide an explicit programming model based on the System.Transactions.Transaction class and an implicit programming model using the System.Transactions.TransactionScope, class where the transactions are automatically managed by the infrastructure.
- Implicit Transaction
- The following piece of code describes how an IBM MQ .NET application puts a message using .NET implicit transaction programming.
Using (TransactionScope scope = new TransactionScope ()) { Q.Put (putMsg,pmo); scope.Complete (); } Q.close(); qMgr.Disconect();}
- Explanation of the code flow of implicit transaction
- The code creates TransactionScope and puts the message under the scope. It then calls Complete to inform the transaction coordinator of the completion of the transaction. The transaction coordinator now issues prepare and commit to complete the transaction. If an issue is detected, then a rollback is called.
- Explicit Transaction
- The following code describes how an IBM MQ .NET application puts messages using .NET explicit transaction programming model.
MQQueueManager qMgr = new MQQueuemanager ("MQQM); MQQueue Q = QMGR.AccessQueue("Q", MQC.MQOO_OUTPUT+MQC.MQOO_INPUT_SHARED); MQPutMessageOptions pmo = new MQPutMessageOptions(); pmo.Options = MQC.MQPMO_SYNCPOINT; MQMessage putMsg1 = new MQMessage(); Using(CommittableTransaction tx = new CommittableTransaction()){ Transaction.Current = tx; try { Q.Put(MSG,pmo); tx.commit(); } catch(Exception) {tx.rollback();} } Q.close(); qMgr.Disconnect(); }
- Explanation of the code flow of explicit transaction
- The piece of code creates transaction using CommitableTransaction class. It puts a message under that scope and then explicitly calls commit to complete the transaction. If there are any issues rollback is called.