ADO.NET 检查是否可以回滚

发布于 2024-07-09 23:00:35 字数 2277 浏览 6 评论 0原文

我问自己是否可以检查 ADO.NET 中当前事务是否可以回滚。

msdn 建议以下实现:

private static void ExecuteSqlTransaction(string connectionString)
{
    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
        {
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Attempt to commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        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);
            }
        }
    }
}

另外还有注释: 回滚事务时应始终使用 Try/Catch 异常处理。 如果连接终止或事务已在服务器上回滚,则回滚会生成 InvalidOperationException。

但我真的不敢相信 try/catch 是检查是否可以回滚的推荐解决方案。

我知道在 SQL Server 实现中,如果事务是“僵尸”,则 SQLTransaction 对象在 Connection 属性上返回 null。

但这是特定于实现的,并且仅适用于 SQL Server。

那么有没有一种独立于数据库的方法来检测事务是否可以回滚呢?

蒂亚 马丁

I'm asking myself if it is possible to check if in ADO.NET the current transaction can be rolled back.

The msdn suggests the following implementation:

private static void ExecuteSqlTransaction(string connectionString)
{
    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
        {
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Attempt to commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        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);
            }
        }
    }
}

Additionally there is the note:
Try/Catch exception handling should always be used when rolling back a transaction. A Rollback generates an InvalidOperationException if the connection is terminated or if the transaction has already been rolled back on the server.

But I can't really believe that a try/catch is the recommended solution to check if a rollback is possible.

I know that in the SQL Server implementation the SQLTransaction objects returns null on the Connection property if the transaction is a "zombie".

But this is quite implementation specific and it only works with SQL Server.

So is there a db-independent way to detect if a transaction can be rolled back?

tia
Martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-07-16 23:00:35

很多这种复杂性是通过在“using”语句中使用 TransactionScope 对象来处理的 - 请在 MSDN. 需要注意的是,当认为有必要时,TransactionScope 会自动“扩展”以使用分布式事务 - 有时这是可取的,有时则不然,因此如果您嵌套 TransactionScope,请务必小心。

A lot of this complexity is handled by using a TransactionScope object in a "using" statement - check it out on MSDN. One caveat is that a TransactionScope will automatically "scale up" to use a distributed transaction when it is deemed necessary - sometimes this is desireable, and other times it isn't, so be careful if you are nesting TransactionScopes.

扛起拖把扫天下 2024-07-16 23:00:35

问题在于,在非 SQL 2005 中,事务范围被提升为分布式事务,这是相当大的开销。

the problem is that in non SQL 2005 the transactionscope is promoted to a distributed transaction which is quite an overhead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文