Creating Transactional Web Pages
One of ASP's most interesting new features is full support for transactional Web pages. Transactional Web pages enable developers to create complete COM+ applications for the Web without the use of components. Transactional Web pages began under IIS 4 but finally reach their true potential under IIS 5. It's now possible to create solutions that have many of the same advantages of component-based solutions but that are written completely in script.
Understanding Transactional Attributes
The key to creating transactional Web pages lies in a special COM+ application named IIS Utilities. This application contains the four ASP ObjectContext components. Each component corresponds to a different transactional attribute.
The secret to starting a transaction in an ASP page is to use the @TRANSACTION directive. This directive is analogous to the transaction properties that you set for a COM+ component in an application. The @TRANSACTION directive is always placed as the first line in any ASP page; otherwise, an error is generated. When you use the directive, the operations performed in any page are treated as a unit and function in much the same way as work performed by a conventional COM+ component. The @TRANSACTION directive has the following values:
@TRANSACTION=REQUIRES_NEW—The ASP page will always initiate a new transaction. This new transaction can enlist other ASP pages if those pages also support transactions.
@TRANSCTION=REQUIRED—The ASP page will initiate a new transaction if one doesn't already exist. However, it might participate in transactions started by other pages.
@TRANSACTION=NOT_SUPPORTED—The ASP page never initiates a transaction. This is the default for all ASP pages that don't specify an @TRANSACTION directive.
@TRANSACTION=SUPPORTED—The ASP page can participate in transactions started by other pages. However, if no transaction exists, a new one won't be started.
When the ASP page is set to start a transaction, all work done by script code in the page is part of the transaction. All work performed by the page will succeed or fail as a batch. Commits and rollbacks of the work are automatic. Similar to a conventional COM+ component, script pages aren't required to specifically call a SetComplete or SetAbort method to commit or roll back transactions. Instead, ASP provides two transaction events that notify the page of success or failure: OnTransactionCommit and OnTransactionAbort. A transactional Web page can code both OnTransactionCommit and OnTransactionAbort event handlers that are automatically executed when the transaction succeeds or fails. This means that you can format response pages to users that indicate success or failure. The code in Listing 1 shows a template that you can use for initiating a single-page transaction.
Listing 1 A Simple Transaction Template
<%@Transaction="REQUIRES_NEW" Language="VBSCRIPT" %> <%Option Explicit%> <HTML> <HEAD> </HEAD> <BODY> <% 'Perform Work 'Database Operation #1 'Database Operation #2 Sub OnTransactionCommit Response.Write "<H1>Success!</H1></BODY></HTML>" End Sub Sub OnTransactionAbort Response.Write "<H1>Failure!</H1></BODY></HTML>" End Sub %>