C#Transaction没有回滚

问题描述 投票:0回答:1
private static void ExecuteSqlTransaction(string connectionString)
{
        while (AreUnprocessedRows())
        {
            DataTable dtItems = GetRowsToProcess(); //Gets 50 rows at a time

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                SqlCommand command = connection.CreateCommand();
                SqlTransaction transaction;

                // Start a local transaction.
                transaction = connection.BeginTransaction("SampleTransaction");

                // Must assign both transaction object and connection
                // to Command object for a pending local transaction
                command.Connection = connection;
                command.Transaction = transaction;

                try
                {
                    for(int i = 0; i < dtItems.Rows.Count; i++)
                    {
                        int intIndex = 0;
                        int.TryParse(Convert.ToString(dtItems.Rows[i][0]), out intIndex);
                        string strDesc = Convert.ToString(dtItems.Rows[i][1]).Trim();

                        command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (" + intIndex + ", '" + strDesc + "')";
                        command.ExecuteNonQuery();

                        command.CommandText = "UPDATE ProcessedTable SET IsUpdated = 1 WHERE TheIndex = " + intIndex;
                        command.ExecuteNonQuery();
                    }

                    // Attempt to commit the transaction.
                    transaction.Commit();
                    //connection.Close(); //Original code has this
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
                    Console.WriteLine("  Message: {0}", ex.Message);

                    // Attempt to roll back the transaction.
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        // This catch block will handle any errors that may have occurred
                        // on the server that would cause the rollback to fail, such as
                        // a closed connection.
                        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                        Console.WriteLine("  Message: {0}", ex2.Message);
                    }
                }
            }
        }
    }

关于代码:

  • 这是一种娱乐,因为实际代码非常大
  • while循环使其保持运行,同时仍有标记为未处理的行
  • 代码获得50行的批处理
  • 提交后,原始代码的连接已关闭

问题:

  • 有时发生数据库死锁,当发生这种情况时,会在Region中插入几行,但不会在ProcessedTable中更新,因此会再次处理它们,导致Region表中出现重复项
  • 为什么Rollback不清理这些半完成交易?
  • 请注意,回滚块中没有异常指示

此外,我可以通过数据库表索引看到在死锁错误后立即重新处理和复制不完整的行

c# sql-server transactions rollback
1个回答
0
投票
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");

// Because of above line, you should rollback like following:
transaction.Rollback("SampleTransaction");
© www.soinside.com 2019 - 2024. All rights reserved.