Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionScope alternative without DTC

are there any alternative to transactionScope which does not need to enable DTC??

In the transaction I need to make two operations:

  1. Create one user (using membership - sql membership provider)
  2. Do one insert operation.
like image 696
Pedre Avatar asked Jun 28 '12 11:06

Pedre


People also ask

Is MSDTC required for SQL Cluster?

MSDTC maintains its log in the shared drive, and it also failed over to the new primary node. In this case, MSDTC can use the log and handle the in-doubt transactions. In case you do not configure MSDTC in the failover cluster, SQL Server uses the MSDTC running locally on the node.

Is MSDTC necessary?

Is MSDTC required? MSDTC is not required for Sql Server installation or operation. If you are only going to use Database Engine, then it is not required or used. If your Sql uses any of the above mentioned query techniques (Linked Server, OPENQUERY, etc), or SSIS or Workstation Components then MSDTC is required.

What is the use of MSDTC in clustering?

MSDTC is a separate Windows service which coordinates distributed transactions across SQL Server instances. When deploying SQL Server in a highly available environment like Windows Failover Clustering, there are certain best practices that can make the MSDTC service's behavior more predictable.

What is TransactionScope in C#?

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.


2 Answers

TransactionScope uses the LTM - Lightweight Transaction Manager in .Net. Only if you open more than one connection in the same transaction or go between databases, should TransactionScope promote the transaction to the 2PC-based TX-manager, DTC.

For MS SQL Server 2008 and above, the DTC will become involved only if you are opening connections to different DBs. OR if you are opening connections in the same transactions from multiple threads EXCEPT if you are using DependentTransaction which is what you should enlist in your global transaction if you want to do threading.

As a side-point: I have some support for the multi-threading story in Castle.Transactions.

Side-point #2: If you use TransactionScope, make sure to declare the IsolationLevel explicitly otherwise you're serializing all your transactions (IsolationLevel.Serializable)!

like image 132
Henrik Avatar answered Oct 11 '22 20:10

Henrik


Add Enlist=false in the connection string of your Membership.

connectionString="Data Source=xxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;Enlist=false"

This is my use case:

using (TransactionScope tScope = new TransactionScope())
{
    MembershipCreateStatus createStatus;
    Membership.CreateUser(model.Email, model.Password, model.Email, null, null, true, model.Id, out createStatus);

    if (createStatus == MembershipCreateStatus.Success)
    {
       Roles.AddUserToRole(model.Email, "Administrator");
       _UpdatePersonnelAccess(model);
       _UpdatePersonnelHasAccess(model);
       _SendEmail_Welcome(model);
       PersonSessionLog.ManageSession(model);
    }
    else
       ViewBag.Message = "Error";

    tScope.Complete();
}

My application is published in Amazon EC2 while the database is in Amazon RDS. DTC is not supported in RDS that's why I also needed a way to prevent escalation to DTC. Btw, I am using SQL Server 2008 R2. I have 2 databases - ASPNETDB, data DB

Thanks to Paul post!

like image 35
danmbuen Avatar answered Oct 11 '22 21:10

danmbuen