TransactionScope 涉及进入范围之前创建的 DbTransaction

发布于 2024-10-10 07:59:21 字数 1563 浏览 5 评论 0原文

我们曾经对一些遗留业务对象使用DAAB,现在我们'我们决定将 TransactionScope 用于一些新的业务对象。现在我们必须在旧业务对象代码中调用新业务对象。当在其中调用新业务对象时,旧业务对象代码将如下所示:

Database db = DatabaseFactory.CreateDatabase();
// create connection instance
DbConnection dbConnection = db.CreateConnection();
dbConnection.Open(); // open connection
// create transaction instance & start the transaction
DbTransaction dbTransaction = dbConnection.BeginTransaction();

// do some old business object insert/update

// now call the new business object
newBO.Update();

// commit transaction
dbTransaction.Commit();

新业务对象中的代码如下:

using (TransactionScope scope = new TransactionScope())
{
    // do some update/insert
    scope.Complete();
}

问题是,这行得通吗?

我目前的信念是,它将基于:

  1. MSDN 文章 说,如果事务是在范围之外创建的,事务的创建者仍然负责提交/回滚它 - 这基本上是我的情况,它会起作用如果这是真的的话就完美了。
  2. 我完成的 TransactionScope 的部分跟踪还表明,在处置 TransactionScope 时不一定会提交提交,但我无法完全完成跟踪,因此我不能 100% 确定我的跟踪是否准确,

但是,有几篇文章关于TransactionScope,包括这篇关于SO的文章,它没有提到这种情况,只是表示事务将被回滚,而不指定事务是否由 TransactionScope 本身创建。

所以我的问题是:根据您的经验,是这种情况吗?

a) 在 TransactionScope 之外创建的事务在处理范围时提交

b) 在 TransactionScope 之外创建的事务在处理时不提交,并且创建者必须负责回滚和处理犯罪?

We used to use DAAB for some legacy business objects and now we've decided to use TransactionScope for some new business objects. Now we have to call the new business object within the old business object code. The old business object code would look like this when the new business object is called within it:

Database db = DatabaseFactory.CreateDatabase();
// create connection instance
DbConnection dbConnection = db.CreateConnection();
dbConnection.Open(); // open connection
// create transaction instance & start the transaction
DbTransaction dbTransaction = dbConnection.BeginTransaction();

// do some old business object insert/update

// now call the new business object
newBO.Update();

// commit transaction
dbTransaction.Commit();

The new business object has code in it as follows:

using (TransactionScope scope = new TransactionScope())
{
    // do some update/insert
    scope.Complete();
}

The question is, is this going to work?

My current belief is that it will work based on:

  1. this MSDN article which says that if the transaction was created outside the scope, the creator of the transaction is still responsible for commit/rolling it back - which is my case basically, it will work perfectly if this is true.
  2. a partial trace of the TransactionScope I complete also indicates that a commit is not necessarily committed on dispose of TransactionScope but I couldn't completely finish the trace so I'm not 100% sure if my trace was accurate

However, there are a few articles on TransactionScope including this post on SO which doesn't mention this scenario and just say that the transaction(s) will be rolled back without specifying if the transaction was created by the TransactionScope itself or not.

So my question: from Your experience, which is the case?

a) transaction created outside of the TransactionScope is committed on the dispose of the scope

OR

b) transaction created outside the TransactionScope is not committed on the disposing and the creator has to take care of rollback & commit?

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

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

发布评论

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

评论(1

北笙凉宸 2024-10-17 07:59:21

答案是 B。但是,您需要记住,transactionScope 使用 MSDTC 在分布式系统级别创建事务,而 DB 事务在 DB 上创建

Answer is B. However, you need to remember, transactionScope create transaction at distributed system level using MSDTC while DB transaction created on DB

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