You are a tasked with performing a code review. The business rule is the following:
* If INSERTs into the first table succeed, then INSERT into the second table.
* However, if the INSERTs into the second table fail, roll back the inserts in the second table but do not roll back the inserts in the first table.
* Although this can also be done by way of regular transactions, It needs to be performed using TransactionScope objects.
Whis code would fit this business rule?
A.
try
{
using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption)
{
….
try
{
…..
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption))
{ …. }
}
}
}
B.
try
{
using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.Required))
{
…
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
{ …. }
……
}
}
C.
try
{
using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.Required))
{
…
}
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
{
….
}
}
D.
try
{
using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.Required))
{
….
try
{
…..
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
{ …. }
}
}
}
Explanation:
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.Transaction Scope (EXAMPLE 3)
(http://msdn.microsoft.com/en-us/library/bb896149%28SQL.100%29.aspx)TransactionScopeOption Enumeration
(http://msdn.microsoft.com/en-us/library/system.transactions.transactionscopeoption.aspx)
Why C does not fit?
C does not meet the requirement of performing the second insert if and only if the first insert succeeds.