TransactionScope 是否可以与预先存在的连接一起使用?

发布于 2025-01-05 12:05:53 字数 721 浏览 3 评论 0原文

我有这样的代码:

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        some_db_function();

        for (i = 0; i < 10; i++)
        {
            some_other_db_function();
        }

        scope.Complete();
    }
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message + " all done transactions will rollback");   
}

在数据库函数内部会发生这样的事情:

private void some_db_functions()
{
    using (TransactionScope scope = new TransactionScope())
    {
       //some processing on db
       scope.Complete();
    }
}

应该是如果数据库事务中存在任何问题,例如在函数中插入或更新时出错;迄今为止已完成的所有交易都将被回滚。但事实并非如此;尽管它抛出异常并且父函数中的scope.Complete()永远不会被触发,但仍然没有任何回滚。

问题出在哪里?

I have a code like this:

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        some_db_function();

        for (i = 0; i < 10; i++)
        {
            some_other_db_function();
        }

        scope.Complete();
    }
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message + " all done transactions will rollback");   
}

and inside the db functions something like this happens:

private void some_db_functions()
{
    using (TransactionScope scope = new TransactionScope())
    {
       //some processing on db
       scope.Complete();
    }
}

It is supposed to be that if there was any problem in the database transactions, like an error inserting or updating in the functions; all the transactions that had been done so far get rolled back. But it does not work like that; and although it throws an exception and the scope.Complete() in the parent function never gets triggered, still nothing get rolled back.

Where is the problem?

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

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

发布评论

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

评论(2

愁杀 2025-01-12 12:05:53

如果打开的连接已经存在,它将不会自动加入环境事务。您必须明确设置它。

不支持隐式登记连接。入伍参加
事务范围,您可以执行以下操作:

在事务范围中打开连接。

或者,如果连接已打开,则调用 EnlistTransaction 方法
在连接对象上。

参考

这将征用现有连接:

connection.EnlistTransaction(Transaction.Current)

If the open connection already exists it will not automatically enlist in an ambient transaction. You would have to set it explicitly.

Implicitly enlisting connections is not supported. To enlist in a
transaction scope, you can do the following:

Open a connection in a transaction scope.

Or, if the connection is already opened, call EnlistTransaction method
on the connection object.

Ref.

This will enlist an existing connection:

connection.EnlistTransaction(Transaction.Current)
︶葆Ⅱㄣ 2025-01-12 12:05:53

IIRC,自动登记到环境事务中发生在连接创建/打开时;如果您在事务范围内创建连接,那么一切都应该很好。然而:

它们都使用先前声明的相同连接

如果该连接存在于事务之外,则它将不会登记。

最佳实践是仅围绕工作单元创建/打开连接,而不是永远创建/打开连接(并且:让连接池完成其工作)。如果你遵循这种做法,它应该会很好地工作。所以:

这不会起作用:

using(var conn = CreateAndOpenConnection()) {
    // ...
    using(var tran = new TransactionScope()) {
        SomeOperations(conn);
        tran.Complete();
    }
    // ...
}

这应该起作用:

using(var tran = new TransactionScope()) {
    // ...
    using(var conn = CreateAndOpenConnection()) {
        SomeOperations(conn);
    }
    tran.Complete();
    // ...
}

IIRC, automatic enlisting into ambient transactions happens at connection creation/opening time; if you create the connection inside the scope of the transaction, all should be good. However:

they are all using the same connection, declared previously

if the connection exists outside of the transaction, it won't enlist.

Best practice is to create/open a connection only around a unit of work, not forever (and: let connection pooling do its job). If you follow that practice, it should work fine. So:

This won't work:

using(var conn = CreateAndOpenConnection()) {
    // ...
    using(var tran = new TransactionScope()) {
        SomeOperations(conn);
        tran.Complete();
    }
    // ...
}

where-as this should work:

using(var tran = new TransactionScope()) {
    // ...
    using(var conn = CreateAndOpenConnection()) {
        SomeOperations(conn);
    }
    tran.Complete();
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文