Tracing Transactions

Sometimes, during development, or perhaps during deployment for logging purposes, you may want to trace the current transaction ID under which your object executes. COM+ provides you with two ways to retrieve the transaction ID, programmatically and administratively, using the Component Services Explorer.

To trace the current transaction ID programmatically, you should use IObjectContextInfo::GetTransactionId( ) . Example 4-8 shows how to trace the current transaction ID to the output window in the debugger.

Example 4-8. Tracing the current transaction ID to the output window

HRESULT hres = S_OK;
GUID guidTransactionID = GUID_NULL;
IObjectContextInfo* pObjectContextInfo = NULL;

hres = ::CoGetObjectContext(IID_IObjectContextInfo,                        
                           (void**)&pObjectContextInfo);

ASSERT(pObjectContextInfo != NULL); //a non-configure object maybe?


hres = pObjectContextInfo->GetTransactionId(&guidTransactionID);


pObjectContextInfo->Release(  );

if(guidTransactionID == GUID_NULL)
{
   ATLTRACE("The object does not take part in a transaction");
}
else
{
   USES_CONVERSION;
   WCHAR pwsGUID[150];
   ::StringFromGUID2(guidTransactionID,pwsGUID,150);
   ATLTRACE("The object takes place in transaction with ID %s ",W2A(pwsGUID));
}

As long as a transaction is in progress, you can view its transaction ID in the Component Services Explorer when using the administrative method. Under the My Computer icon in the Component Services Explorer is the Distributed Transaction Coordinator (DTC) folder. Expand the DTC folder and select the Transaction List item. The right pane in the Component Services Explorer contains a list of all the transactions executing on your machine (see Figure 4-14).

The transaction list view

Figure 4-14.  The transaction list view

The Component Services Explorer presents a few bits of information on every transaction. The Status column contains the type of the root component and the status of the transaction, and the Unit of Work ID column contains the transaction ID.

Get COM & .NET Component Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.