为什么使用 TransactionScope 和 MSDTC 的 VS 数据库单元测试失败?

发布于 2024-07-20 04:36:05 字数 627 浏览 3 评论 0原文

我们使用 DatabaseTestFixture< 在 SQL Server 2000 数据库上运行一些单元测试/a> 类使用未提交的 TransactionScope,因此所有更改都会回滚。 测试在本地数据库上运行没有问题。

然后,我们将单元测试指向通用数据库服务器,并在运行单元测试的计算机上设置 MSDTC,该计算机一切正常。

现在,我们已经设置了一台新机器,它将针对相同的公共数据库运行单元测试。 单元测试没有在这台机器上运行,我们得到以下错误:

System.Transactions.TransactionException:事务已隐式或显式提交或中止。

MSDTC 设置与正在运行的设置完全相同,我们逐屏进行了比较。

以前有人经历过这样的事情吗? 或者有任何指示我们可以在哪里寻找可能导致此问题的线索吗?

我们检查了Windows版本和服务包、防火墙选项、msdtc选项、VS版本和服务包。

We have some unit tests running against a SQL server 2000 database using the DatabaseTestFixture class which uses a TransactionScope that is not committed and therefore all changes are rolled back. The tests ran against a local database with no problem.

We then pointed the unit tests at a common database server and setup MSDTC on the machine running the unit tests and all is well with that machine.

Now though we have setup a new machine which will be running the unit test also against the same common database. The unit test do not run on this machine, we get the following error:

System.Transactions.TransactionException : The transaction has already been implicitly or explicitly committed or aborted.

The MSDTC settings are exactly the same as the one that is working, we have compared screen by screen.

Has anyone experienced something like this before? Or got any pointers on where we might look for clues as to what might be causing it?

We have checked Windows versions and service packs, firewall options, msdtc options, VS versions and service packs.

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

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

发布评论

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

评论(2

月亮是我掰弯的 2024-07-27 04:36:05

这似乎与 NUnit 2.5 有关。
在此之前,以下代码运行良好:

[SetUp]
public void SetUp() {
    this._testDataContext = new DataContext();
    this._transactionScope = new TransactionScope();
}

[TearDown]
public void TearDown() {
    if (_transactionScope != null)
    {
        this._transactionScope.Dispose();
    }
    if (_testDataContext != null)
    {
        _testDataContext.Dispose();
    }
}

在 2.5 中,事务范围需要使用 TransactionScopeOption.RequiresNew 进行实例化。 我认为要么它没有被正确处理,要么 NUnit 在运行测试时正在运行一些环境事务。
将 SetUp 方法的第二行更改为

this._transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);

,它应该可以正常工作。

如果有人能够更好地解释为什么会发生这种情况,我很想听听。

This seems to be related to NUnit 2.5.
Before it, the following code worked fine:

[SetUp]
public void SetUp() {
    this._testDataContext = new DataContext();
    this._transactionScope = new TransactionScope();
}

[TearDown]
public void TearDown() {
    if (_transactionScope != null)
    {
        this._transactionScope.Dispose();
    }
    if (_testDataContext != null)
    {
        _testDataContext.Dispose();
    }
}

With 2.5 the transaction scope needs to instanciated with TransactionScopeOption.RequiresNew. I assume that either it isn't being disposed correctly, or that NUnit has some ambient transaction running when the tests are being run.
Change the second line on the SetUp method to

this._transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);

and it should work correctly.

If someone manages to get a better explanation as to why this happens, I'd love to hear it.

绮筵 2024-07-27 04:36:05

此问题可能与您使用的 Nunit 版本有关。 我在运行单元 2.5 时遇到了完全相同的问题,但当我使用 nunit 2.4.8 或 2.2 运行单元测试时,问题神秘地消失了。

请尝试这个,让我知道,这样我就可以提高我微不足道的个人资料积分!

问候

MAC

this problem is probably related to the version of Nunit you're using. I had exactly the same issue when running unit 2.5 but the problem mysteriously disappears when I run the unit tests with nunit 2.4.8 or 2.2.

Please try this any let me know so I boost my puny profile points!

Regards

MAC

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