TransactionScopeOption vs TransactionScopeOptions
TransactionScopeOption in System.Transaction
What is the differences between TransactionScopeOptions when we use it to create TransactionScope.
We have the following option at our disposal.
1. TransactionScopeOption.Required
TransactionScope created here is dependent on the root TransactionScope.
2. TransactionScopeOption.RequiresNew
Creates an entirely new TransactionScope
3. TransactionScopeOption.Suppress
TransactionScope created here is independent of root TransactionScope.
Code below shows an example of how transactionScope takes effect when you stop your application in s1.Complete();
Data gets committed in TransactionScopeMixRequiresNew and TransactionScopeMixSupress method but not TransactionScopeMixRequires.
using (TransactionScope s1 = new TransactionScope(TransactionScopeOption.Required))
{
TransactionScopeMixRequires();
TransactionScopeMixRequiresNew();
TransactionScopeMixSupress();
/// if you stop your application here
/// TransactionScopeMixRequiresNew and TransactionScopeMixSupress's data
/// gets committed into the database
s1.Complete();
}
Implementing TransactionScope is pretty straight forward. However, my testing so far tells me that TransactionScope run without hitting "Communication with the underlying transaction manager has failed" if your Application / Database server reside on the same domain.
1. TransactionScopeOption.Required
TransactionScope created here is dependent on the root TransactionScope.
2. TransactionScopeOption.RequiresNew
Creates an entirely new TransactionScope
3. TransactionScopeOption.Suppress
TransactionScope created here is independent of root TransactionScope.
Code below shows an example of how transactionScope takes effect when you stop your application in s1.Complete();
Data gets committed in TransactionScopeMixRequiresNew and TransactionScopeMixSupress method but not TransactionScopeMixRequires.
using (TransactionScope s1 = new TransactionScope(TransactionScopeOption.Required))
{
TransactionScopeMixRequires();
TransactionScopeMixRequiresNew();
TransactionScopeMixSupress();
/// if you stop your application here
/// TransactionScopeMixRequiresNew and TransactionScopeMixSupress's data
/// gets committed into the database
s1.Complete();
}
Implementing TransactionScope is pretty straight forward. However, my testing so far tells me that TransactionScope run without hitting "Communication with the underlying transaction manager has failed" if your Application / Database server reside on the same domain.
When i try running the application from a remote location, it fails badly. I had to stick to the same domain.
Notes
To use TransactionScope, there are a few requirements that i've noticed.
a) Use a single connection. You can use the using() block if you going to execute several sql continously and then immediately close your connection. Otherwise, maintain your connection open throughout the TransactionScope. Finally call transactionScope method's called Completed.
b) If you have external connection, declared outside out the transaction block that won't work either.
For example :-
SqlConnection OUTSIDECONNECTION = new SqlConnection(myConnectionString);
using (TransactionScope scope = new TransactionScope())
{
///// do something external OUTSIDECONNECTION ////
//// create you own connection instance ////
scope.Complete();
}
DTC Troubleshooting link
I found this very useful.
Comments