Solved

how to test TransactionScope

Posted on 2012-03-20
3
347 Views
Last Modified: 2012-04-09
i have a wrapper class for TransactionScopes being used in multiple places, one of them is the root.  what is the best way to test my nested TransactionScopes actually work?  For example, i want to make sure that when my wrapper's Displose() is called, all TransactionScopes are now null.  also, what is the best way to find out which TransactionScope i am looking at, via System.Thread.Current?
0
Comment
Question by:mmingfeilam
  • 2
3 Comments
 
LVL 11

Expert Comment

by:b_levitt
ID: 37752861
Can you describe why you need this (it might help me answer the question)?  TransactionScope already supports nesting so I'm trying to understand what you are doing to determine if the test you are looking for simply tests the functionality of TransactionScope.
0
 

Author Comment

by:mmingfeilam
ID: 37779812
yes you are right, i am simply doing the unenviable, testing out someone else's code.  the wrapper class is to make sure all TransactionScope is using the ReadCommitted isolation level.
0
 
LVL 11

Accepted Solution

by:
b_levitt earned 500 total points
ID: 37783550
You want to test the read commited isolation level vs other isolation levels?  Or you simply want to confirm that a transaction is properly being rolled back?

A basic test would be (basics like connecting to the db have been omited):
Int32 id;
using(TransactionScope transaction = new TransactionScope())
{
  
  id = MyBusinessClass.ExecuteMyMethod()
  
  //query db to check for new records:
  SqlCommand cmd = new SqlCommand("select 1 from mytable where id=" + id.ToString());
  //test should be 1 and not null
  Object test = cmd.ExecuteScalar()

  //don't complete so the transaction is rolled back
  //transaction.Complete()
}
//query db to check for new records:
SqlCommand cmd = new SqlCommand("select 1 from mytable where id=" + id.ToString());
//test should now be null since transaction has been rolled back.
Object test = cmd.ExecuteScalar()

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Web.Config using IIS 7.5? 4 35
Hey!! 5 33
.NET 2008 VB and C# 6 27
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 17
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question