IDb事务回滚超时

发布于 2024-07-30 08:34:25 字数 540 浏览 1 评论 0原文

我正在处理一个有趣的情况,我在单个事务中执行许多数据库更新。 如果这些更新因任何原因失败,事务将回滚。

IDbTransaction transaction
try {
    transaction = connection.BeginTransaction();

    // do lots of updates (where at least one fails)

    transaction.Commit();
} catch {
    transaction.Rollback(); // results in a timeout exception
} finally {
    connection.Dispose();
}

我相信上面的代码通常被认为是在事务中执行数据库更新的标准模板。

我面临的问题是,虽然 transaction.Rollback() 被发送到 SQL Server,但它在客户端上也超时了。

无论如何,是否可以区分发出回滚命令的超时和该命令执行完成的超时?

提前致谢, 本

I am dealing with an interesting situation where I perform many database updates in a single transaction. If these updates fail for any reason, the transaction is rolled-back.

IDbTransaction transaction
try {
    transaction = connection.BeginTransaction();

    // do lots of updates (where at least one fails)

    transaction.Commit();
} catch {
    transaction.Rollback(); // results in a timeout exception
} finally {
    connection.Dispose();
}

I believe the above code is generally considered the standard template for performing database updates within a transaction.

The issue I am facing is that whilst transaction.Rollback() is being issued to SQL Server, it is also timing out on the client.

Is there anyway of distinguishing between a timeout to issue the rollback command and a timeout on that command executing to completion?

Thanks in advance,
Ben

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

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

发布评论

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

评论(1

一腔孤↑勇 2024-08-06 08:34:25

您应该指定特定的捕获,即查看引发的不同异常。 如果在处理 sql 命令时出现错误,则应抛出 SqlException,以便捕获不同的异常以在项目中进行区分。

此外,您还可以捕获并编程

异常 - 尝试提交事务时发生错误。

InvalidOperationException - 事务已提交或回滚。
-或者-
连接已断开。

安德鲁

You should specify particular catches i.e. Look at the different exceptions that are thrown. If there is an error whilst processing the sql command then a SqlException should be thrown so catch the different exceptions to differentiate in your project.

Further more you could also catch and program for

Exception - An error occurred while trying to commit the transaction.

InvalidOperationException - The transaction has already been committed or rolled back.
-or-
The connection is broken.

Andrew

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