0
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 行
  • 原始代码在提交后关闭了连接

问题:

  • 有时会发生数据库死锁,当这种情况发生时,几行被插入到区域中但没有更新,ProcessedTable因此它们被再次处理导致Region表中的重复
  • 为什么Rollback清理这些半途而废的交易?
  • 请注意,回滚块中没有异常的迹象

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

4

1 回答 1

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

// Because of above line, you should rollback like following:
transaction.Rollback("SampleTransaction");
于 2018-09-07T18:04:46.533 回答