12.4 Create a Transactional Web Method


Problem

You need to execute all the actions in a Web method within the context of a single COM+ transaction so that they all either fail or succeed as a unit.

Solution

Enable an automatic transaction by choosing a value from the System.EnterpriseServices.TransactionOption enumeration and applying it to the TransactionOption property of the WebMethod attribute.

Discussion

In ASP.NET, XML Web services include support for automatic transactions that can be enabled on a per-method basis. When enabled, any data source that supports COM+ transactions (which includes most databases) is automatically enlisted in the current transaction when it's used in your code. The transaction is automatically committed when the Web method completes. The transaction is rolled back if any unhandled exception occurs or if you explicitly call the SetAbort method of the System.EnterpriseServices.ContextUtil class.

To enable transaction support for a Web method, set the TransactionOption property of the WebMethod attribute to RequiresNew . For example, the transactional Web method shown in the following code deletes records in a database and then explicitly resets the transaction. To use this code, you must add a reference to the System.EnterpriseServices.dll assembly.

 using System; using System.Data.SqlClient; using System.Web.Services; using System.EnterpriseServices; public class TransactionTest {     private static string connectionString = "Data Source=localhost;" +       "Initial Catalog=Northwind;user ID=sa";     [WebMethod(TransactionOption=TransactionOption.RequiresNew)]     public void FailedTransaction() {         // Create the connection.         SqlConnection con = new SqlConnection(connectionString);         // Create the command for filling the DataSet.         SqlCommand cmd = new SqlCommand("DELETE * FROM Customers", con);         // Apply the update.         // This will be automatically registered as part of the transaction.         con.Open();         cmd.ExecuteNonQuery();         con.Close();         // Call another method.         DoSomething();         // (If no errors have occurred, the database changes         // are committed here when the method ends).     }     private void DoSomething() {         // Vote to abort the message.         ContextUtil.SetAbort();     } } 

You can use the Component Services console to monitor this transaction. You can start the Component Services utility by selecting Component Services from the Administrative Tools section of the Control Panel. In the Component Services utility, select the Distributed Transaction Coordinator for the current computer and view the Transaction Statistics. Figure 12.2 shows how the display will look after running this code, which produces one failed transaction.

click to expand
Figure 12.2: Monitoring a failed transaction.

Because of the stateless nature of the HTTP protocol, an XML Web service method can participate only as the root of a transaction, which means that you can't enlist more than one Web method in a single transaction. Although the TransactionOption property accepts all the standard TransactionOption values, the values don't have the expected meanings. For example, Disabled , NotSupported , and Supported all have the same effect: they disable transaction support. Similarly, Required and RequiresNew both enable transaction support and start a new transaction. I recommend that you use RequiresNew in your Web methods because its name most clearly matches the actual behavior.

Note  

COM+ transactions work seamlessly with most data sources because they provide compatible resource managers. But always remember, if you interact with a nontransactional resource, your code won't be rolled back. Some examples of actions that aren't transactional include writing a file, placing information into session state, and accessing a hardware device (such as a printer). On the other hand, data operations with most enterprise database systems (including Microsoft SQL Server and Oracle) are COM+ compatible.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net