You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application connects to two different Microsoft SQL Server 2008 database servers named Server1
and Server2. A string named sql1 contains a connection string to Server1. A string named sql2
contains a connection string to Server2.
01 using (TransactionScope scope = new
02 …
03 )
04 {
05 using (SqlConnection cn1 = new SqlConnection(sql1))
06 {
07 try{
08 …
09 }
10 catch (Exception ex)
11 {
12 }
13 }
14 scope.Complete();
15 }
You need to ensure that the application meets the following requirements:
There is a SqlConnection named cn2 that uses sql2.
The commands that use cn1 are initially enlisted as a lightweight transaction.
The cn2 SqlConnection is enlisted in the same TransactionScope only if commands executed by cn1
do not throw an exception.
What should you do?
A.
Insert the following code segment at line 02.
TransactionScope(TransactionScopeOption.Suppress)
Insert the following code segment at line 08.
using (SqlConnection cn2 = new SqlConnection(sql2))
{
try
{
cn2.Open();
…
cn1.Open();
…
}
catch (Exception ex){}
}
B.
Insert the following code segment at line 02.
TransactionScope(TransactionScopeOption.Suppress)
Insert the following code segment at line 08.
cn1.Open();
…
using (SqlConnection cn2 = new SqlConnection(sql2))
{
try
{
cn2.Open();
…
}
catch (Exception ex){}
}
C.
Insert the following code segment at line 02.
TransactionScope(TransactionScopeOption.RequiresNew)
Insert the following code segment at line 08.
using (SqlConnection cn2 = new SqlConnection(sql2))
{
try{
cn2.Open();
…
cn1.Open();
…
}
catch (Exception ex){}
}
D.
Insert the following code segment at line 02.
TransactionScope(TransactionScopeOption.RequiresNew)
Insert the following code segment at line 08.
cn1.Open();
…
using (SqlConnection cn2 = new SqlConnection(sql2))
{
try
{
cn2.Open();
…
}
catch (Exception ex){}
}
Explanation:
Seen in exam
Here cn1 is for the Ambient Transaction (i.e the lightweight or logical transaction) that will be used
run the 2 transactions in the ambient scope.
If the cn1 transaction fails, then the requirement is for the cn2 transaction NOT to join the ambient
transaction. It needs to run within its own independent transaction. This is achieved by using
TransactionScopeOption.
Suppress.
If the cn2 transaction does NOT fail, then both transactions will run under the ambient Transaction.
TransactionScopeOption
Required A transaction is required by the scope. It uses an ambient transaction if one already exists.
Otherwise, it creates a new transaction before entering the scope. This is the default value.
RequiresNew A new transaction is always created for the scope.
Suppress The ambient transaction context is suppressed when creating the scope. All operations
within the scope are done without an ambient transaction context.