SubSonic2.2 SharedDbConnectionScope 和 TransactionScope 事务混淆

发布于 2024-08-02 08:51:58 字数 1838 浏览 7 评论 0原文

啊啊!!!

围绕 SharedDbConnectionScope 和 TransactionScope 对象似乎有点混乱,以支持将 SubSonic 查询包装在事务中。

文档建议指定 using SharedDbConnectionScope 围绕 using TransactionScope...

using(SharedDbConnectionScope scope = new SharedDbConnectionScope())
{
  using(TransactionScope ts = new TransactionScope())
  {
    // do something
    ts.Complete();
  }
}

然后这里还有其他问题,例如 Subsonic:将 SharedDbConnectionScope 与 TransactionScope 一起使用似乎被破坏了表明文档是错误的,这两个对象应该是相反的......

using(TransactionScope ts = new TransactionScope())
{
  using(SharedDbConnectionScope scope = new SharedDbConnectionScope())
  {
    // do something
    ts.Complete();
  }
}

但查看源代码我更加使困惑。

在 SqlQuery.cs 代码文件中,它有许多 ExecuteTransaction 重载。例如...

public static void ExecuteTransaction(List<SqlQuery> queries)
{
  using(SharedDbConnectionScope scope = new SharedDbConnectionScope())
  {
    using(TransactionScope ts = new TransactionScope())
    {
      foreach(SqlQuery q in queries)
        q.Execute();
    }
  }
}

嗯...有趣...与文档匹配,但是... ts.Complete() 调用在哪里?

应该如何提交事务?据我所知,它总是会回滚。对于所有 ExecuteTransaction 重载来说都是一样的!

但这是真正的亮点...

在 TransactionWithDtcOffTests.cs 代码中,有一些很好的测试,除了他们以另一种方式设置了 SharedDbConnectionScope 和 TransactionScope!

using(TransactionScope ts = new TransactionScope())
{
  using(SharedDbConnectionScope connScope = new SharedDbConnectionScope())
  {
    // <snip />
  }
}

我还没有机会运行 SubSonic 2.2 的测试,但我假设有人已经通过了。

最后...

有人能给我关于 SubSonic2.2 中的事务应该如何进行的明确答案吗?被设置?这些文档确实是错误的吗? ExecuteTransaction 重载和测试的源是否与实际正确的方式一致?

ARGH!!!

There seems to be a little confusion surrounding the SharedDbConnectionScope and TransactionScope objects to enable wrapping your SubSonic queries within a transaction.

The docs suggest specifying the using SharedDbConnectionScope wrapped around the using TransactionScope...

using(SharedDbConnectionScope scope = new SharedDbConnectionScope())
{
  using(TransactionScope ts = new TransactionScope())
  {
    // do something
    ts.Complete();
  }
}

Then other question here such as Subsonic: Using SharedDbConnectionScope together with TransactionScope seems to be broken suggest the docs are wrong and the two objects should be the other way around...

using(TransactionScope ts = new TransactionScope())
{
  using(SharedDbConnectionScope scope = new SharedDbConnectionScope())
  {
    // do something
    ts.Complete();
  }
}

But looking into the source code I am even more confused.

In the SqlQuery.cs code file it has a number of ExecuteTransaction overloads. For example...

public static void ExecuteTransaction(List<SqlQuery> queries)
{
  using(SharedDbConnectionScope scope = new SharedDbConnectionScope())
  {
    using(TransactionScope ts = new TransactionScope())
    {
      foreach(SqlQuery q in queries)
        q.Execute();
    }
  }
}

Umm... Interesting... Matches the docs but... Where's the ts.Complete() call?

How is that supposed to commit the transaction? As far as I can see it will always rollback. And it is the same for all the ExecuteTransaction overloads!

But here is the real kicker...

In the TransactionWithDtcOffTests.cs code has some nice tests except they have set up the SharedDbConnectionScope and TransactionScope around the other way!

using(TransactionScope ts = new TransactionScope())
{
  using(SharedDbConnectionScope connScope = new SharedDbConnectionScope())
  {
    // <snip />
  }
}

I haven't had the opportunity to run the tests for SubSonic 2.2 but I assume someone has and they passed..

Finally...

Can someone give me the definitive answer to how Transactions in SubSonic2.2 should be set up? Are the docs indeed wrong? Does the source for the ExecuteTransaction overloads and tests be aligned to whichever way is actually correct?

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

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

发布评论

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

评论(2

栖竹 2024-08-09 08:51:58

SharedConnectionScope (SCS) 块必须位于 TransactionScope (TS) 内。
SCS 的目的是在可能的情况下防止将事务升级到 MSDTC,因此将 TS using 块放在 SCS using 块内对我来说没有什么意义。
无论如何,每个 TS 块都必须有一个 Complete() 调用才能提交事务。

The SharedConnectionScope (SCS) block must be inside a TransactionScope (TS).
The purpose of the SCS is to prevent escalating the transaction to the MSDTC if possible, so having the TS using block inside of a SCS using block makes little sense to me.
In any case, every TS block must have a Complete() call for the transaction to be committed.

要走干脆点 2024-08-09 08:51:58

我个人发现,当使用SQL 2005时,SCS必须在TS内
当使用 SQL 2000(带有 MSDTC)时,SCS 必须包装 TS。
我希望这有帮助...

Personally i found that when using SQL 2005, SCS must be inside TS
and when using SQL 2000 (with MSDTC), SCS must wrap TS.
I hope this helps...

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