using TransactionScope : System.Transactions.TransactionAbortedException: The transaction has aborted

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 03:51:01
Rodolfo Grave

If you use TransactionScope and you:

  • open more than one connection to a database and
  • are connecting to a SQL Server 2005 server

the transaction will be escalated to DTC. Check this other SO question: TransactionScope automatically escalating to MSDTC on some machines?

The solution is either:

  • Use SQL Server 2008 or
  • Use SqlTransaction instead of TransactionScope just like the former answer suggests:

    using (var conn = new SqlConnection(connectionString))
    {  
        using (var tx = conn.BeginTransaction())
        {
            FirstMethod(conn);
            SecondMethod(conn);
            tx.Commit();
        }
    }
    
ratneshsinghparihar

i can propose to you a better way to achieve your goal. there should be a single transaction for 2 DB call per connection.

it should be like

using (SqlConnection conn1 = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"))
{ 
    using (conn1.BeginTransaction()
    {
        try
        {
            FirstMethod(Conn1);
            SecondMethod(Conn2);
        }
        catch()
        {
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!