Wednesday, September 8, 2010

ASP Transactions

The ObjectContext object is used to commit or abort transactions. For an .asp page to commit transaction, @TRANSACTION directive should be present in the script.

Transactions are important to maintain data integrity, among other things, and have been used with databases for some time now. Luckily, transactions aren't restricted to databases - you can use them in Active Server Pages as well, and without having to create custom components using Microsoft Transaction Server (MTS). This article will take a look at what transactions are, how they will help ASP developers, and how to implement them.

The ObjectContext object is used to commit or abort transactions. For an .asp page to commit transaction, @TRANSACTION directive should be present in the script.

Methods

SetAbort : Aborts the transaction initiated by the ASP script. 

SetComplete : Declares that there is no reason for the transaction not to complete. So if all the components taking part in the transaction also call SetComplete method then the transaction will complete.

Events

OnTransactionAbort : This event occurs when the transaction is aborted.

OnTransactionCommit: This event occurs when the transactional script's transaction is committed.

So what does transaction ASP code look like? The answer is, exactly like regular ASP code. We must simply add a one line directive to the beginning code to get it to behave properly:

<%@ TRANSACTION = value %>

Typically, each ASP page will be its own transaction, however, it is possible to continue transactions across more than one page by using the Server.Transfer and Server.Execute methods (new to IIS 5). If the calling page is transacted, and the current page uses "Transaction = Required" or "Transaction = Supported" then the current page will continue the existing transaction, which makes some very complex applications possible. 

Now you simply write ASP code as you would normally, and things will work transactionally.

<%@ TRANSACTION = Required %>

<% strText = Request.form("Textbox") if strText = "" then ObjectContext.SetAbort Response.write("You did not enter the text
")
else
Response.write("Processing transaction...
")
end if
%>

<%
'commit and rollback code
sub OnTransactionCommit()
Response.write("Operation was successful")
End Sub
sub OnTransactionAbort() 
Response.write("Operation was unsuccessful")
End Sub 
%>

No comments:

Post a Comment