Data Access and 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
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.