This documentation is archived and is not being maintained.

Sending Messages Within Internal Transactions

Internal transactions involve only Message Queuing resources and are managed entirely from within Message Queuing with the MessageQueueTransaction Class. You instantiate an instance of MessageQueueTransaction to handle the transaction process and pass it to the message you are choosing to send as a transaction.

The following methods control the transactional process:

MethodUsed to
BeginIndicate the start of a transaction that consists of one or more messages and operations.
CommitUsed to commit the transaction if all of the message operations it contained were performed successfully.
AbortUsed during error-checking to roll back a transaction.
Note   A disconnected or full queue is not considered a failure.

In most cases, your transactional code should be enclosed within a Try...Catch statement. This allows you to specify the transaction in the Try portion of the statement, and then use the Catch statement to issue the Abort if any errors occur. See the code below for an example of this pattern.

To send messages within an internal transaction

  1. Create an instance of the MessageQueue component and set its Path property to the queue to which you want to refer. For more information, see Creating MessageQueue Component Instances.
  2. Create an instance of the MessageQueueTransaction class to control the transaction.
  3. Call the Begin method of the MessageQueueTransaction class.
  4. Define each message you want to send after calling Begin. For more information, see Creating MessageQueue Component Instances.
  5. After the last message is sent, call Commit on the MessageQueueTransaction instance to close the transaction.
  6. Enclose the entire transaction in Try...Catch error-checking code to handle any errors that arise.
  7. If you detect an error, roll back the transaction by calling the Abort method.

    Your code might look like this:

    ' Visual Basic
    Dim transaction As New System.Messaging.MessageQueueTransaction
    Try
        transaction.Begin()
        MessageQueue1.Send("Message1", "Label1", transaction)
        MessageQueue1.Send("Message2", "Label2", transaction)
        MessageQueue1.Send("Message3", "Label3", transaction)
        transaction.Commit()
    Catch
        transaction.Abort()
    End Try
    
    // C#
    System.Messaging.MessageQueueTransaction transaction = 
        new System.Messaging.MessageQueueTransaction();
    try 
    {
        transaction.Begin();  
        messageQueue1.Send("Message1", "Label1", transaction);
        messageQueue1.Send("Message2", "Label2", transaction);
        messageQueue1.Send("Message3", "Label3", transaction);
        transaction.Commit();
    }
    catch 
    {
        transaction.Abort();
    }
    

See Also

Transactional Message Processing | Creating Transactional Queues | Creating MessageQueue Component Instances | Sending and Serializing Messages

Show: