Share via


How to: Send Messages Within Internal Transactions 

Internal transactions involve only Message Queuing resources and are managed entirely from within Message Queuing with the MessageQueueTransaction. 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:

Method Used to

Begin

Indicate the start of a transaction that consists of one or more messages and operations.

Commit

Commit the transaction if all of the message operations that it contained were performed successfully.

Abort

Roll back a transaction during error-checking.

NoteNote

A disconnected or full queue is not considered a failure.

In most cases, your transactional code should be enclosed within a Try...Catch...Finally Statement (Visual Basic) (or try-catch-finally (C# Programmers Reference)). This allows you to specify the transaction in the Try portion of the statement, and then use the Catch statement to issue the Abort method 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 How to: Create 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 How to: Create MessageQueue Component Instances.

  5. After the last message is sent, call Commit on the MessageQueueTransaction instance to close the transaction.

  6. Enclose the entire work of the 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:

    Dim transaction As New System.Messaging.MessageQueueTransaction
    transaction.Begin()
    Try
        MessageQueue1.Send("Message1", "Label1", transaction)
        MessageQueue1.Send("Message2", "Label2", transaction)
        MessageQueue1.Send("Message3", "Label3", transaction)
        transaction.Commit()
    Catch
        transaction.Abort()
    End Try
    
    System.Messaging.MessageQueueTransaction transaction =
        new System.Messaging.MessageQueueTransaction();
    transaction.Begin();
    try
    {
        messageQueue1.Send("Message1", "Label1", transaction);
        messageQueue1.Send("Message2", "Label2", transaction);
        messageQueue1.Send("Message3", "Label3", transaction);
        transaction.Commit();
    }
    catch
    {
        transaction.Abort();
    }
    
    System.Messaging.MessageQueue messageQueue1 =  new System.Messaging.MessageQueue();
    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(System.Exception exp)
    {
       transaction.Abort();
    }
    

See Also

Tasks

How to: Create Transactional Queues
How to: Create MessageQueue Component Instances

Concepts

Transactional Message Processing

Other Resources

Sending and Serializing Messages