How To Span a Transaction Across Multiple Active Server Pages (299638)



The information in this article applies to:

  • Microsoft Active Server Pages 3.0
  • Microsoft Internet Information Server 5.0

This article was previously published under Q299638

SUMMARY

This step-by-step procedure demonstrates how to use the @TRANSACTION directive so that a transaction spans across multiple Active Server Pages (ASP) pages. If you want to span a transaction across multiple ASP pages, you must use the @TRANSACTION directive for each of the pages that will participate in the transaction.

back to the top

Prerequisites

This list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:

  • Microsoft Active Server Pages, version 3.0
  • Microsoft Internet Information Server 5.0
  • Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
This article assumes that you are familiar with the following topics:

  • Creating Active Server Pages
  • Transactions
back to the top

Spanning Transactions Across Multiple ASP Pages

  1. In Notepad, create a new ASP page named Tran1.asp, and paste the following code (in which the Transaction directive is set to Required):
    <%@ TRANSACTION=Required %>
    <%
    
        Server.Execute "Tran2.asp"
    
        Sub OnTransactionCommit()
            	Response.Write "<p><b>The Transaction just committed</b>." 
    	Response.Write "This message came from the "
            	Response.Write "OnTransactionCommit() event handler."
        End Sub
    
        Sub OnTransactionAbort()
            	Response.Write "<p><b>The Transaction just aborted</b>." 
            	Response.Write "This message came from the "
            	Response.Write "OnTransactionAbort() event handler."
        End Sub
    
    %>
    						
    NOTE: When the Transaction directive is set to Required, the ASP page starts a transaction if one does not exist or joins an existing parent transaction.
  2. On the File menu, click Save. In the Save in drop-down list box, click the C:\Inetpub\Wwwroot folder. In the Save as type drop-down list box, click All Files. In the File name list box, type Tran1.asp. Close the file.
  3. In Notepad, create a new ASP page named Tran2.asp, and paste the following code (in which the Transaction directive is set to Supported):
    <%@ TRANSACTION=Supported %>
    <%
    	ObjectContext.SetComplete
    %>
    						
    NOTE: When the Transaction directive is set to Supported, the ASP page joins the parent transaction, which Tran1.asp starts. This is also referred to as enlisting-in-transaction.
  4. On the File menu, click Save. In the Save in drop-down list box, click the C:\Inetpub\Wwwroot folder. In the Save as type drop-down list box, click All Files. In the File name list box, type Tran2.asp. Close the file.
  5. In your Web browser, run Tran1.asp (for example, go to http://localhost/Tran1.asp). Notice that Tran1.asp (with the Required transaction value) starts a transaction and then runs Tran2.asp (with the Supported transaction value), which calls the ObjectContext.SetComplete method to commit the transaction. The following output is returned:

    The Transaction just committed.This message came from the OnTransactionCommit() event handler.

  6. In Tran2.asp, change the following code
    ObjectContext.SetComplete
    						
    to:
    ObjectContext.SetAbort
    						
    and save the file.
  7. In your browser, run Tran1.asp again. Notice that the following output is returned, which shows that the transaction that aborted in Tran2.asp is propagated to Tran1.asp:

    The Transaction just aborted.This message came from the OnTransactionAbort() event handler.

back to the top

Troubleshooting

The @TRANSACTION directive must appear on the first line in the ASP page and is usually included with the @LANGUAGE directive. If anything appears before this in the page, a script error occurs.

back to the top

REFERENCES

For more information about the @TRANSACTION directive, see the following MSDN article: back to the top

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbHOWTOmaster kbTransaction KB299638 kbAudDeveloper