Sending Messages Using an Internal Transaction

To send a message using an internal transaction, call MQBeginTransaction to initiate the transaction and call MQSendMessage to send the message.

To send a message using an internal transaction
  1. Call MQBeginTransaction to initiate internal transaction.
    ITransaction  *pTransaction;
    hr = MQBeginTransaction (&pTransaction);   // Pointer to a pointer
                                               // to the transaction  
                                               // object.
    
  2. Call MQSendMessage to send message.
    hr = MQSendMessage(h,           // Handle to destination queue.
                       &msgprops,   // Pointer to MQMSGPROPS structure.
                       pTransaction); // Pointer to transaction object.
    
  3. Commit or abort the transaction.
    hr = pTransaction->Commit(0, 0, 0);
    -or-
    hr = pTransaction->Abort(0, 0, 0);
    
  4. Release the transaction object.
    pTransaction->Release();
    

Example

This example sends a single message within an internal transaction.

void TransactSend(QUEUEHANDLE h, MQMSGPROPS * pMsgProps)
{
      HRESULT hr;      
      printf ("\nStarting transaction...\n\n");

      ///////////////////////////////////////
      // Call MQBeginTransaction to initiate 
      // the internal transaction.
      ///////////////////////////////////////
      ITransaction  *pTransaction;
      hr = MQBeginTransaction (&pTransaction);   // Pointer to a 
                                                 // pointer to the 
                                                 // transaction object

      if (FAILED(hr))
      {
         Error ("BeginTransaction",hr);
      }

      ////////////////////////////////////
      // Set default to commit the 
      // transaction.
      ////////////////////////////////////
      BOOL fCommit = TRUE;

      ////////////////////////////////////
      // Within the transaction: Call 
      // MQSendMessage to send the message to
      // the Receiver Side.
      //////////////////////////////////////
      hr = MQSendMessage(h,              // Handle to destination
                                         // queue.
                         &msgprops,      // Pointer to MQMSGPROPS
                                         // structure.
                         pTransaction);  // Pointer to transaction 
                                         // object.

      if (FAILED(hr))
      {
         printf("\nFailed in MQSendMessage(). hresult- %lxh\n", (DWORD) hr) ;
         fCommit = FALSE;  // Aborting as MQSendMessage failed.
      }

      ////////////////////////////////////
      // Commit or abort the transaction.
      ////////////////////////////////////
      if (fCommit)
      {
         printf ("Committing the transaction...   ");
         hr = pTransaction->Commit(0, 0, 0);
         if (FAILED(hr))
            printf ("Failed... Transaction aborted.\n\n");
         else
            printf ("Transaction committed successfully.\n\n");
      }
      else
      {
         printf ("Aborting the transaction...   ");
         hr = pTransaction->Abort(0, 0, 0);
         if (FAILED(hr))
            Error("Transaction Abort",hr);
         else
            printf ("Transaction aborted.\n\n");
      }
      
      //////////////////////////
      // Release the transaction.
      //////////////////////////
      pTransaction->Release();

  }

© 1997 by Microsoft Corporation. All rights reserved.