ASP Transaction Basics

As mentioned earlier, to have ASP take advantage of the reliability provided by MTS services, you need to include only the @TRANSACTION directive in your script. This directive tells MTS that any changes that occur in that page—such as database manipulation or Microsoft Message Queue Server (MSMQ) message transmission—should be considered transactions. A change that is being managed by transaction services can be either committed, making it more or less permanent, or aborted, which would result in rolling back to the state of the database or queue before the changes were made.

The code in Figure 19-2 shows a simple example of how to add transactional behavior to your ASP Web pages. The code starts with the @TRANSACTION directive. This must be the first line of code within the ASP Web page or an error will be generated. The transaction attributes that are available are shown in Table 19-2.

Figure 19-2. ASP code showing the @TRANSACTION directive to declare an ASP script as transactional.

<% @TRANSACTION=Required LANGUAGE="VBScript" %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" <TITLE>Simple Transactional ASP Page</TITLE> </HEAD> <BODY BGCOLOR="White" topmargin="10" leftmargin="10"> <h2><font color="navy">Simple Transactional ASP Page</font></h2> <hr> This is a simple example demonstrating the basic structure of a transactional ASP Page.  </BODY> </HTML> <% ' The Transacted Script Commit Handler. This sub-routine ' will be called if the transacted script commits. ' Note that in the example above, there is no way for the ' script not to commit. Sub OnTransactionCommit()     Response.Write "<p><b>The Transaction just committed</b>."      Response.Write "<br>This message came from the "     Response.Write "OnTransactionCommit() event handler." End Sub ' The Transacted Script Abort Handler. This sub-routine ' will be called if the transacted script aborts. ' Note that in the example above, there is no way for the ' script not to commit. Sub OnTransactionAbort()     Response.Write "<p><b>The Transaction just aborted</b>."      Response.Write "<br>This message came from the "     Response.Write "OnTransactionAbort() event handler." End Sub %> 

Table 19-2. Transaction attributes for the @TRANSACTION directive in ASP Web pages.

Value Meaning
Requires_New Starts a new transaction
Required Starts a new transaction
Supported Does not start a transaction
Not_Supported Does not start a transaction

The code in Figure 19-2 specifies that a transaction is required, so a new transaction is started for the page. You'll also notice two subroutines within the page. These are the event handlers for the OnTransactionCommit and OnTransactionAbort events for the ObjectContext object. They provide a way to trap the event and take an appropriate action. In this example, the event handlers merely print a message to the browser (standard output) stating the success or failure of the transaction. Of course, in this example, there is no way for the script not to commit. The listing is included on the companion CD-ROM as the asp_trans.asp file within the VI-Bank/VIntDev98 folder of the VI-Bank sample Web project. Figure 19-3 shows the resulting output after the code has been run within the browser.

click to view at full size.

Figure 19-3. The browser output resulting from running the code shown in Figure 19-2.

Just to prove to yourself that the ASP Web page listed above was truly included as a transaction, try pulling up the Transaction Server Explorer application. Access this program group by choosing Start|Programs|Windows NT 4.0 Option Pack|Microsoft Transaction Server|Transaction Server Explorer from the task bar. After you have started Transaction Server Explorer, choose Transaction Statistics from the left pane. Each time you run the ASP example, you'll see the Committed and Total counters in the Aggregate section increase by one. Figure 19-4 shows the Transaction Server Explorer window.

You'll notice in Figure 19-4 that the Transaction Server Explorer also shows you the minimum, average, and maximum response times for the transactions. The response time is the duration of the transaction in milliseconds from the moment it begins to the moment when it is committed. It does not count transactions that are aborted. Looking at the transaction statistics is a good way to monitor how quickly your transactions are executing. It's worth allowing as many transactions to occur as possible before you start looking at the response times in order to determine performance data. This will give you better statistical results. After 50 to 100 transactions, you should start to get a clear picture of the average MTS performance for your particular applications.

click to view at full size.

Figure 19-4. The Transaction Server Explorer showing the total number of committed and aborted transactions.

To give an example of how you might use the event handlers in real life, suppose your transaction aborted for some reason. Now MTS is capable of only rolling back changes made to the database. It does not roll back changes to files on your hard disk, ASP session and application variables, and so on. The OnTransactionAbort event handler would be an ideal place to roll back these types of changes.

You can also explicitly commit or roll back transactions using the SetCommit or SetAbort methods of the ObjectContext object. Therefore, a transaction is committed when either the script has successfully completed or the ObjectContext.SetComplete method has been called. Likewise, the transaction is aborted when the script either encounters some kind of processing error, the script times out, or the ObjectContext.SetAbort method has been called. The code in Figure 19-5 illustrates all of these scenarios.

Figure 19-5. ASP code (asp_trans_demo.asp) illustrating four different processing scenarios for transactional ASP.

<%@ TRANSACTION=Required LANGUAGE="VBScript" %> <% ' VI 6.0 Scripting Object Model Enabled %> <!--#include file="_ScriptLibrary/pm.asp"--> <% if StartPageProcessing() Then Response.End() %> <FORM name=thisForm METHOD=post> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Simple Transactional Web Page using ASP</TITLE> </HEAD> <BODY BGCOLOR="white" topmargin="10" leftmargin="10" > <h2><font color="navy"> Simple Transactional Web Page Using ASP </font></h2> <hr> <FORM METHOD=post ACTION="asp_transaction.asp"> <table cellpadding="5", cellspacing="5"> <tr> <td> <!-- Radiobutton Design-Time Control --> <!--METADATA TYPE="DesignerControl" startspan <OBJECT classid="clsid:B5F0E45D-DC5F-11D0-9846-0000F8027CA0"      height=39 id=TransactionGroup      style="HEIGHT: 39px; LEFT: 0px; TOP: 0px;      WIDTH: 102px" width=102>     <PARAM NAME="_ExtentX" VALUE="3254">     <PARAM NAME="_ExtentY" VALUE="1693">     <PARAM NAME="id" VALUE="TransactionGroup">     <PARAM NAME="DataSource" VALUE="">     <PARAM NAME="DataField" VALUE="">     <PARAM NAME="ControlStyle" VALUE="0">     <PARAM NAME="BType" VALUE="0">     <PARAM NAME="Enabled" VALUE="-1">     <PARAM NAME="Visible" VALUE="-1">     <PARAM NAME="Platform" VALUE="256">     <PARAM NAME="UsesStaticList" VALUE="-1">     <PARAM NAME="CLSize" VALUE="4">     <PARAM NAME="CLED1" VALUE="Commit">     <PARAM NAME="CLEV1" VALUE="1">     <PARAM NAME="CLED2" VALUE="Forced Commit">     <PARAM NAME="CLEV2" VALUE="2">     <PARAM NAME="CLED3" VALUE="Forced Abort">     <PARAM NAME="CLEV3" VALUE="3">     <PARAM NAME="CLED4" VALUE="Syntax Error">     <PARAM NAME="CLEV4" VALUE="4">     <PARAM NAME="LocalPath" VALUE=""></OBJECT> --> <!--#INCLUDE FILE="_ScriptLibrary/OptionGrp.ASP"--> <SCRIPT LANGUAGE=JavaScript RUNAT=Server> function _initTransactionGroup() {     TransactionGroup.addItem('Commit', '1');     TransactionGroup.addItem('Forced Commit', '2');     TransactionGroup.addItem('Forced Abort', '3');     TransactionGroup.addItem('Syntax Error', '4'); } function _TransactionGroup_ctor() {     CreateOptionGroup('TransactionGroup',          _initTransactionGroup, null); } </script> <% TransactionGroup.display %> <!--METADATA TYPE="DesignerControl" endspan--> </td> <td> <font size="2"> This example shows four scenarios as follows:  <UL>     <LI>Commit - The page commits automatically     <LI>Forced Commit - A commit is forced using      ObjectContext.SetComplete     <LI>Forced Abort - An abort is forced using      ObjectContext.SetAbort     <LI>Syntax Error - A syntax error is used to trigger an      abort</LI> </UL> </font> </td> </tr> <tr> <td align="center"> <INPUT type="submit" value="Submit" id=submit name=submit>  </FORM> </td> <td> </td> </tr> </table> <% RequestMethod = Request.ServerVariables("REQUEST_METHOD") If RequestMethod = "POST" Then     Select Case Request.Form("TransactionGroup")         Case 1             ' Do nothing         Case 2             ' Force a commit             ObjectContext.SetComplete         Case 3             ' Force an abort             ObjectContext.SetAbort         Case 4             ' Force an abort via a syntax error             Microsoft.Rules     End Select End If %> </BODY> <% ' VI 6.0 Scripting Object Model Enabled %> <% EndPageProcessing() %> </FORM> </HTML> <%     ' The Transacted Script Commit Handler. This subroutine     ' will be called if the transacted script commits.     Sub OnTransactionCommit()         If RequestMethod = "POST" Then             Response.Write _                 "<p><b>The Transaction just committed</b>."              Response.Write "<br>This message came from the "             Response.Write "OnTransactionCommit() event handler."         End If     End Sub     ' The Transacted Script Abort Handler. This subroutine     ' will be called if the transacted script aborts.     Sub OnTransactionAbort()         If RequestMethod = "POST" Then             Response.Write "<p><b>The Transaction just aborted</b>."              Response.Write "<br>This message came from the "             Response.Write "OnTransactionAbort() event handler."         End If     End Sub %> 

NOTE
It's important to know that a transaction cannot span multiple ASP Web pages. If a transaction requires objects from several transactional components, you should group operations that use those objects into one ASP Web page.

In Figure 19-5, the code allows you to select a scenario using the radio buttons (created using an OptionGroup design-time control) and then to submit the page in order to have that scenario executed on the server. The event handlers report the resulting commit or abort back to the user. The four scenarios included are as follows:

  • Commit The page is allowed to commit automatically with no programmatic intervention.
  • Forced Commit A commit is forced using the ObjectContext.SetComplete syntax within the ASP Web page.
  • Forced Abort An abort is forced using the ObjectContext.SetAbort syntax within the ASP page.
  • Syntax Error A syntax error within the ASP page is used to trigger an abort.

The code for Figure 19-5 is included on the CD-ROM as the asp_trans_demo.asp file in the VI-Bank/VIntDev98 folder. Figure 19-6 shows the output from the page when the Forced Abort radio button has been chosen.

click to view at full size.

Figure 19-6. The browser output resulting from running the code shown in Figure 19-5 and choosing a "Forced Abort" via the ObjectContext.SetAbort syntax.



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