. NET Distributed Transaction processing summary "under"-distributed transactions that contain MSMQ

Source: Internet
Author: User
Tags message queue msmq

Transferred from: http://www.cnblogs.com/daxnet/archive/2011/03/15/1984995.html

. NET provides access support for MSMQ directly, you can easily manipulate MSMQ by adding system.messaging assembly references. MSMQ supports two transactional modes: internal transaction processing and MS-DTC-based distributed transaction processing.

Internal transaction processing for MSMQ

Internal transaction processing for MSMQ means that transaction processing is done only with the transaction mechanism provided by MSMQ itself. For example, if you have a series of messages that need to be published to MSMQ, you can start an internal transaction to ensure that the publishing process for these messages is an atomic operation. To use the internal transaction mechanism of MSMQ, when creating a message queue, you need to tick the "transactional" option, as shown in:

First, you need to create an MessageQueueTransaction object and use the begin call to start the internal Transact-processing of MSMQ. Then, in the MessageQueue send method, the message is sent using the overloaded function of Send (object, messagequeuetransaction). The created MessageQueueTransaction object is passed as the second argument to the Send method, and the commit method of the MessageQueueTransaction object is used to commit the transaction after all messages have been sent. If a problem is encountered during the sending of a message, the abort call to the MessageQueueTransaction object is used to rollback the transaction. See the sample code below:

Hide line number copy code ? Internal transaction processing for MSMQ
  1.     False, False, queueaccessmode.sendandreceive))
  2. {
  3.     MessageQueueTransaction trans = new MessageQueueTransaction ();
  4.     Try
  5.     {
  6.         Trans. Begin ();
  7.         for (int i = 0; i < 5; i++)
  8.         {
  9.             Messagequeue.send (New Message (i), trans);
  10.         }
  11.         Trans.commit ();
  12.     }
  13.     Catch
  14.     {
  15.         Trans. Abort ();
  16.     }
  17.     Messagequeue.close ();
  18. }

Note: If your message queue does not have the "transactional" option set when it is created, you will not be able to modify this option after the creation of the message queue. Even worse, executing the above code on a non-transactional queue does not publish the message to the message queue, and the framework itself does not prompt any error messages indicating that the message was not published successfully.

Using MSMQ in Distributed transaction processing

In the context of distributed transactions (for example, in the TransactionScope of. NET 2.0+), the messagequeuetransaction mentioned above will be useless, that is to say, MessageQueueTransaction has nothing to do with distributed transaction processing. All you have to do is initialize an instance of MessageQueue in a normal way, and then call the Send method to publish the message and, when you publish the message, tell MessageQueue by setting the value of MessageQueueTransactionType. is currently in the context of a distributed transaction. Therefore, you need to use the overloaded method of Send/receive: Send (object, MessageQueueTransactionType) and receive (MessageQueueTransactionType). As follows:

Hide line number copy code ? MSMQ calls in a distributed transaction
    1. using (TransactionScope transaction = new TransactionScope ())
    2. {
    3.     Message inputmsg = inputqueue.receive (messagequeuetransactiontype.automatic);
    4.     Do some
    5.     Transaction.complete ();
    6. }

Note: For some relatively long-life transactions, for example, suppose your use case is this: you first need to get the message from a message queue and then update your database records, then your code might look like this:

Hide line number copy code ? MSMQ calls in a distributed transaction
    1. using (TransactionScope transaction = new TransactionScope ())
    2. {
    3.     using (MessageQueue somequeue = new MessageQueue ("<queue connection>"))
    4.     {
    5.         Message msg = somequeue.receive ();
    6.         Do something else
    7.     }
    8.     Transaction.complete ();
    9. }

It's not right to do this! Because the Receive method is a synchronous call, if there is nothing at all in the message queue, the receive call is blocked until a new message appears in the message queue. This means that your distributed transactions are always in an open state, and may cause timeouts due to long wait times, resulting in a messagequeueexception.

The correct approach is to use the BeginPeek call on MessageQueue (note: Not the BeginReceive method, because the BeginReceive method is not used to process the transactional queue), and then subscribe to the Peekcomplete event, In the process of event processing, the use of TransactionScope and receive methods to achieve the acquisition of messages. For example:

Hide line number   copy code  
    1. MessageQueue inputqueue = new MessageQueue ("<queue connection>");
    2. Inputqueue.peekcompleted + = (s, e) = =
    3.     {
    4.         using (TransactionScope transaction = new TransactionScope ())
    5.         {
    6.             Message inputmsg = inputqueue.receive (messagequeuetransactiontype.automatic);
    7.             Do some
    8.             Transaction.complete ();
    9.         }
    10.         Inputqueue.beginpeek ();
    11.     };
    12. Inputqueue.beginpeek ();

Finally, to remind you that if you want to do things that are limited to dealing with MSMQ, you can use the internal transaction processing mechanism of MSMQ, after all, using Distributed transaction processing involves MS-DTC, which can lead to excessive overhead and impact performance.

. NET Distributed Transaction processing summary "under"-Distributed transaction processing with MSMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.