Gokul's Blog

System.Transaction Scope

Leave a comment

We can wrap a set of operations under a transaction scope by using System.Transaction Scope.

Checkout a nice article on how to implement this by Rob Conery: blog.wekeroad.com/blog/Using-SubSonic-Within-Transactions/

Example code as implemented by Rob:

//use the product object to test the transaction scope
using (TransactionScope scope = new TransactionScope
     try {
 
         Product p1 = new Product(1);
 
         p1.QuantityPerUnit = "10 boxes x 30 bags";
 
         p1.Save("Unit Test");
 
         Product p2 = new Product(2);
 
         p2.QuantityPerUnit = "24 - 42 oz bottles";
 
         p2.Save("Unit Test");
 
         Product p3 = new Product(3);
 
         p3.QuantityPerUnit = "12 - 5500 ml bottles";
 
         p3.Save("Unit Test");
 
         scope.Complete();
 
     } catch (System.Data.SqlClient.SqlException x) {
 
         //trap/trace/log as needed
 
         throw x;
     }
 
  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s