You can use the ObjectContext object to either commit or abort a transaction, managed by Microsoft Transaction Server (MTS), that has been initiated by a script contained in an ASP page.
When an ASP contains the @TRANSACTION directive, the page runs in a transaction and does not finish processing until the transaction either succeeds completely or fails.
ObjectContext.method
SetComplete | The SetComplete method declares that the script is not aware of any reason for the transaction not to complete. If all components participating in the transaction also call SetComplete, the transaction will complete. |
SetAbort | The SetAbort method declares that the transaction initiated by the script has not completed and the resources should not be updated. |
OnTransactionCommit |
---|
OnTransactionAbort |
ObjectContext implements two methods of the MTS ObjectContext object. The SetAbort method explicitly aborts the transaction. This causes MTS to prevent any updates to resources that were contacted during the first phase of the transaction. When the transaction aborts, the script's OnTransactionAbort event will be processed.
Calling the SetComplete method does not necessarily mean that the transaction is complete. The transaction will only complete if all of the transactional components called by the script call SetComplete. In most instances, you will not need to call SetComplete within the script, as the script is assumed to be complete if it finishes processing without calling SetAbort.
ObjectContext exposes six methods in addition to SetAbort and SetComplete. These other methods are not available to ASP scripts, however, they are available to components called by the script.
The following example uses the SetAbort and SetCommit methods. The Sales.htm file obtains data required to process a sales request. The second file, SalesVerify.asp contains a script that uses two objects, Inventory and Sales to process the sale. SetAbort is called if Inventory returns an error code indicating that there is not sufficient inventory on hand to process the sale. If the Inventory object does not return the error code, SetComplete is called and the sale is processed.
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<TITLE>Sales Order</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><FONT FACE="ARIAL,HELVETICA">
<H2>Sales Order Form </H2>
<FORM METHOD=POST ACTION="SalesVerify.asp">
<P>Please enter the product code, quantity, and your account number.
<INPUT TYPE=TEXT NAME=QuantityToBuy>
<INPUT TYPE=TEXT NAME=ProductCode>
<INPUT TYPE=TEXT NAME=AccountIn>
<P>
<INPUT TYPE=SUBMIT>
</FONT>
</BODY>
</HTML>
<%@ Transaction = Required %>
<%
Set CurrentQOH = Server.CreateObject("Mycomp.Inventory")
Set CurrentSales = Server.CreateObject("Mycomp.Sales")
CheckQuantity = Request("QuantityToBuy")
CheckProduct = Request("ProductCode")
QuantityStatus = CurrentQOH.CheckQOH(CheckQuantity,CheckProduct)
If QuantityStatus = None
ObjectContext.SetAbort
Response.Write "Sorry, there is not sufficient quantity on hand to process your sale."
Else
ObjectContext.SetComplete
Account = Request("AccountIn")
Saleupdate = CurrentSales.PostIt(AccountIn)
End If
%>
Creating Transactional Scripts in Web Applications