Data Access and Transactions

Previous Topic Next Topic

Participating in Transactions

You can create a transactional component that takes advantage of the benefits of a COM+ application, with only a few extra lines of code.

To create a transactional component

  1. Call GetObjectContext to get a reference to the ObjectContext object, which enables your component to “vote” on the success or failure of the transaction in progress.
  2. Create other components by using the ObjectContext object’s CreateInstance method. When a base client instantiates an object by using the CreateInstance method, the new object and its descendants will participate in the transaction, unless the new object’s transaction attribute is set to Requires a new transaction or Does not support transactions.
  3. Call either SetComplete when the object has completed successfully, or SetAbort to cancel the transaction.
Just-In-Time Activation

This discussion about component state really only makes sense within the scope of a transaction. But what happens to the object once the transaction has been completed? Component Services deactivates it. Whenever an application calls SetComplete, whether from an ASP page or from within a COM component, the system recycles all the objects involved in the transaction—even stateful ones. In the process of deactivation, the object’s member variables are reinitialized to their initial values.

This process is part of just-in-time activation, which allows the Component Services run-time environment to free up object resources, including any database connections it holds, without requiring the application to release its references to component objects. Components are activated only as needed for executing requests from clients, allowing otherwise idle server resources to be used more productively. Just-In-Time activation allows your application to conserve system resources as it scales up to multiple users.

This is yet another reason to be careful about maintaining state in objects. Clients of a stateful COM component object must be aware of how it uses SetComplete to ensure that any state the object maintains won’t be needed after the object undergoes just-in-time activation.


The following Visual Basic code template demonstrates how to incorporate these elements into a component:

Sub DoMTSTransaction()
Dim objCtx As ObjectContext
Dim objNew As NewObject

On Error Goto ErrHandler
Set objCtx = GetObjectContext()
Set objNew = objCtx.CreateInstance("MyObject.NewObject")

   '---------------------------------
'--- More component logic here ---
   '---------------------------------

objCtx.SetComplete
Exit Sub

ErrHandler:
objCtx.SetAbort
End Sub

Be sure not to use CreateObject or GetObject when creating objects in a COM component. Role-based security in Component Services works only if you use Server.CreateObject from ASP or ObjectContext.CreateInstance from within your COM component. When you use CreateObject or GetObject, the component identity is inherited from the application process. Conversely, when you use Server.CreateObject, the identity is that of the impersonated user. In order for COM role-based security to work properly, the correct caller identity must be determined.

For more information about COM security and roles in Component Services, see Security in this book. For more examples of how to create transactional components, refer to the Exploration Air sample site included on the Resource Kit companion CD.


© 1997-1999 Microsoft Corporation. All rights reserved.