Understanding Transactions and COM+ Services

A transaction in the eCommerce world involves a series of operations where if one operation fails, the whole transaction fails. Transaction processing is used to update databases reliably. When you are making many related changes to a database or updating several databases at once, you want to ensure that all of the changes are correctly executed. If any of the changes fail, you want to restore the original state of the database tables.

For example, when you buy a book from an online store, the operations may include updating the store's inventory, verifying that your credit card number is valid, charging your credit card, and initiating the shipment of your book. If any one of these operations fails, all changes must be cancelled or reversed. This technology, called transaction processing, is handled by COM+.

Transaction processing is available to COM components and to ASP pages. Implementation in COM components involves calling the right COM interfaces. Implementation in ASP pages involves using the @TRANSACTION directive and the ASP built-in object called ObjectContext Object. Transaction processing in an ASP page can work with transaction processing in a COM component so long as the component is called from within the ASP page that starts the transaction.

Without Component Services, you would have to write your scripts and components to manually track the requested changes and restore data if any changes failed. With Component Services, you simply declare your scripts and components to require transactions and let Component Services handle the transaction coordination. Transaction processing applies only to database access; Component Services cannot roll back changes to the file system or changes to other, non-transactional resources. The database your application accesses must be supported by Component Services. Currently Component Services supports SQL Server and any database that supports the XA protocol from the X/Open consortium. Component Services will continue to expand its support for other databases in the future.

Using the Server.Transfer and Server.Execute methods a transaction can span multiple ASP pages. If a script contains the @TRANSACTION directive, with the value specified as Required, and the script is called by either the Server.Transfer or Server.Execute method, then the script will continue the transaction of the calling .asp file if the calling .asp file was transacted. If the calling .asp file was not transacted, the called .asp file will then automatically create a new transaction.

For example, the following script initiates a transaction:

<%@ TRANSACTION=Required %> 

<% 
  . 
  . 
  .  
  'End transaction. 
  Server.Transfer("/BookSales/EndTrans.asp")         
%> 
However, following script calls another script that also initializes a transaction:


<%@ TRANSACTION=Required%> 

<% 
  'Instantiate a custom component to close transactions. 
  Set objSale = Server.CreateObject("SalesTransacted.Complete") 
  . 
  . 
  . 
%> 

However, the interaction between the two scripts would constitute only a single transaction. For more information about writing scripts with Server.Transfer and Server.Execute, see Sending Content to the Browser.