Share via


Visual Basic Code Example: Checking Transaction Boundaries

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

The following example provides a Sub procedure that reads all the messages in the queue to determine which message was the first message sent within a specific transaction, what subsequent message were sent as part of the transaction, and which message was the last message sent in the transaction.

The following three message properties are retrieved for each message.

To check transaction boundaries

  • Define the structures needed to retrieve messages.

Code Example

The following code example requires MSMQ 3.0.

Sub XactVerifyBoundaries( _  
                         strPathName As String _  
                         )  
  Dim q As MSMQQueue  
  Dim qinfo As New MSMQQueueInfo  
  Dim msg As MSMQMessage  
  Dim CurrTransArray(19) As Byte  ' Needed to check the transaction ID  
  Dim fInTrans As Boolean  
  Dim iC As Integer  
  
  On Error GoTo ErrorHandler  
  qinfo.PathName = strPathName  
  On Error Resume Next  
  qinfo.Create IsTransactional:=True  
  On Error GoTo ErrorHandler  
  Set q = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)  
  MsgBox "Entering the first wait."  
  
  fInTrans = False  
  Do While True  
    Set msg = q.Receive  
    If msg.IsFirstInTransaction2 Then  
      If Not fInTrans Then  
        If msg.IsLastInTransaction2 Then  
          ' The message is both the first and last message. A single message  
          ' was sent within the transaction.  
          MsgBox "A single-message transaction."  
        Else  
          'OK - Starting a new transaction  
          For iC = 0 To 19  
            CurrTransArray(iC) = msg.TransactionId(iC)  
          Next iC  
          fInTrans = True  
          MsgBox "A new transaction has begun, the message was received."  
        End If  
      Else  
        'Error condition: Starting a new transaction before  
        ' the previous transaction has been closed.  
        MsgBox "Error: A new transaction has started before the current one has ended."  
        Exit Sub  
      End If  
    Else  
      If Not fInTrans Then  
        ' Error condition: The message was received outside of the transaction boundaries.  
        MsgBox "Error: The message received outside of the transaction boundaries."  
        Exit Sub  
      End If  
      ' Matching the transaction ID.  
      For iC = 0 To 19  
        If Not (CurrTransArray(iC) = msg.TransactionId(iC)) Then  
          ' Error condition: The transaction ID does not match.  
          MsgBox "Error: The transaction ID changed during a transaction."  
          Exit Sub  
        End If  
      Next iC  
      If msg.IsLastInTransaction2 Then  
        ' The last message in the transaction was received.  
        fInTrans = False  
        MsgBox "The transaction has ended "  
      Else  
        ' We received a message within the transaction.  
        MsgBox "A message contained within the transaction was received."  
      End If  
    End If  
  
  Loop  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub