17.10 Using Transactions in Web Services

 <  Day Day Up  >  

You want to roll back any database commands that have executed if a subsequent command fails.


Technique

A transaction comes from the concept of creating a safe method of updating data to ensure data integrity. For instance, if a Web Service implements a method that updates several tables within a database, the possibility for data corruption is high should one of the updates fail. In this case, the Web Service should use a transaction to ensure that any database changes are rolled back to their initial state if any update fails.

To make database updates occur within a transaction in a Web Service method, add the TransactionOption parameter to the WebMethod attribute. Listing 17.8 shows an example of updating two tables within a database in which one table does not exist, thereby throwing an exception. Once the second database update occurs, the database is automatically rolled back. In other words, the change that took place before the second update executed is reversed .

Listing 17.8 Using TransactionOption for Transactions
 [WebMethod(Description="This is a transaction sample.",      TransactionOption=TransactionOption.RequiresNew)] public void DeleteValueByID( int ID ) {     string firstCmdString = "DELETE FROM TestTable WHERE ID='" + ID + "'" ;     string secondCmdString = "DELETE FROM NonExistentTable WHERE ID='" + ID + "'";     SqlCommand firstCmd = new SqlCommand(firstCmdString, sqlConnection1);     SqlCommand secondCmd = new SqlCommand(secondCmdString, sqlConnection1);     // execute first product deletion     firstCmd.Connection.Open();     firstCmd.ExecuteNonQuery();     // execute second command. When this throws an exception then the     // first command is automatically rolled back since we're     // participating in a transaction     secondCmd.ExecuteNonQuery();     sqlConnection1.Close(); } 

Comments

Transactions are implemented within the System.EnterpriseServices namespace. The EnterpriseServices namespace contains several classes that interact with COM+ services, in which transactions play a role. The TransactionOption enumerated data type does a lot of behind-the-scenes work to enable transactions. Rather than require you to write all the necessary code to commit or roll back a transaction, the TransactionOption attribute automatically creates a transaction object that is accessed by the .NET data provider. If the provider finds this transaction object within your object's context, it participates by enlisting with the COM+ Distributed Transaction Coordinator. One thing to note is that using this attribute assumes the Web Service is being accessed remotely by a connecting client. If instead a client adds a normal assembly reference to your Web Service and calls the method directly, the transactional behavior does not occur.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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