The ObjectContext Object

An MTS object is an instance of an MTS component. Remember that an MTS component is a COM component that is installed into MTS. On disk it is a Microsoft ActiveX DLL. The component can contain any number of methods and usually provides a certain category of service. For example, a finance component might contain many different types of financial methods and is therefore a bundling of a particular category of business logic.

MTS maintains context for each object. This context, which is implicitly associated with the object, contains information about the object's execution environment, such as the identity of the object's creator and, optionally, the transaction encompassing the work of the object. The object context is similar in concept to the process context that an operating system maintains for an executing program.

An MTS object and its associated context object have corresponding lifetimes. MTS creates the context before it creates the MTS object. MTS destroys the context after it destroys the MTS object.

The ObjectContext Object and ASP

In Internet Information Server 3.0, the ASP framework provided five built-in objects: Application, Session, Request, Response, and Server. The built-in objects are special because they are built into ASP Web pages and do not need to be created before you can use them in scripts. For example, to send output to the browser you use the Write method of the Response object. You simply call Response.Write and pass it a string. You don't have to create an instance of this object before using its methods.

In IIS 4.0, there is now a sixth built-in object: the ObjectContext object. You can use the ObjectContext object either to commit or to abort a transaction managed by MTS that has been initiated by a script contained in an ASP Web page.

When an ASP Web page contains the @TRANSACTION directive, the page runs in a transaction and does not finish processing until the transaction either succeeds completely or fails. We'll find out more about the @TRANSACTION directive later on in this chapter.

Methods

The ObjectContext object has 11 methods that can be called from your applications. These methods allow you to carry out a number of tasks, including:

  • Declaring that the object's work is complete
  • Preventing a transaction from being committed
  • Instantiating other MTS objects
  • Finding out if a caller is in a particular role
  • Finding out if security is enabled
  • Finding out if the object is executing within a transaction
  • Retrieving IIS built-in objects

Table 19-1 lists the methods that can be found in the ObjectContext object.

Table 19-1. Methods of the ObjectContext object.

Method Description
Count Returns the number of context object properties.
CreateInstance Instantiates another MTS object.
DisableCommit Declares that the object hasn't finished its work and that its transactional updates are in an inconsistent state. The object retains its state across method calls, and any attempts to commit the transaction before the object calls EnableCommit or SetComplete will result in the transaction being aborted.
EnableCommit Declares that the object's work isn't necessarily finished, but its transactional updates are in a consistent state. This method allows the transaction to be committed, but the object retains its state across method calls until it calls SetComplete or SetAbort, or until the transaction is completed.
IsCallerInRole Indicates whether the object's direct caller is in a specified role (either directly or as part of a group).
IsInTransaction Indicates whether the object is executing within a transaction.
IsSecurityEnabled Indicates whether security is enabled. MTS security is enabled unless the object is running in the client's process.
Item Returns a context object property.
Security Returns a reference to an object's SecurityProperty object.
SetAbort Declares that the object has completed its work and can be deactivated on returning from the currently executing method, but that its transactional updates are in an inconsistent state or that an unrecoverable error occurred. This means that the transaction in which the object was executing must be aborted. If any object executing within a transaction returns to its client after calling SetAbort, the entire transaction is doomed to abort.
SetComplete Declares that the object has completed its work and can be deactivated on returning from the currently executing method. For objects that are executing within the scope of a transaction, it also indicates that the object's transactional updates can be committed. When an object that is the root of a transaction calls SetComplete, MTS attempts to commit the transaction on return from the current method.

Not all of these methods are available to all Microsoft Visual Studio development tools. Table 19-1 actually lists the methods available to Visual Basic. Visual C++ and Visual J++ gain access to the current object's context through the IObjectContext interface, as we shall see later on. Active Server Pages has access to only two of the methods above: SetComplete and SetAbort.

Events

Two events can occur after an ObjectContext method has executed: OnTransactionAbort and OnTransactionCommit.

The OnTransactionAbort event occurs if the transaction is aborted. When the OnTransactionAbort event occurs, IIS will process the script's OnTransactionAbort subroutine, if it exists.

As you might expect, the OnTransactionCommit event occurs after a transacted script's transaction commits. When the OnTransactionCommit event occurs, IIS will process the script's OnTransactionCommit subroutine, if it exists.

We'll see some examples of how to use these events within your ASP script later on in this chapter.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net