Linq to SQL 事务插入然后选择真的非常慢

发布于 2024-11-09 09:35:30 字数 1820 浏览 0 评论 0原文

我正在开发一个系统,该系统基本上将数据从一组表迁移到另一组表。一切工作正常,但我决定使用事务,而不是仅仅在部分完成的事情上失败。 (也就是说,如果发生一些异常,我想回滚而不是迁移部分数据。)

我有一个服务(以三层架构方式,而不是Web),它在数据访问层上开始事务。数据上下文在包含许多方法的数据访问类中共享。这些方法使用各种 LINQ-to-SQL 技术来更新/插入/删除。所有 LINQ-to-SQL“选择”都在 CompiledQueries 中。

“BeginTransaction”方法启动一个事务,如下所示:

Public Sub BeginTransaction() Implements ITransactionalQueriesBase.BeginTransaction
    Me.Context.Connection.Open()
    Me.Context.Transaction = Context.Connection.BeginTransaction()
    IsInTransaction = True
End Sub

基本上,我编写了一个测试,该测试启动事务,插入表,然后尝试检索刚刚插入的值,所有这些都在事务期间进行。我这样做是因为我想断言插入方法实际上尝试插入。然后,在测试期间我会回滚,然后进行测试以确保新插入的值实际上并未提交到表中。测试看起来像这样:

<TestMethod()>
    Public Sub FacilityService_Can_Rollback_A_Transaction()
        faciService.BeginTransaction()

        Dim devApp = UnitTestHelper.CreateDevelopmentApplication(devService.GetDevelopmentType("NEWFACI").ID, 1, 1, 1, 1)

        Dim devInsertRes = devService.InsertDevelopmentApplication(devApp)

        Assert.IsTrue(devInsertRes.ReturnValue > 0)

        For Each dir1 In devInsertRes.Messages
            Assert.Fail(dir1)
        Next

        Dim migrationResult = faciService.ProcessNewFacilityDevelopment(devInsertRes.ReturnValue)
        Assert.IsTrue(migrationResult.ReturnValue.InsertResult)

        Dim faciRetrieval1 = faciService.GetFacilityByID(migrationResult.ReturnValue.FacilityID)
        Assert.IsNotNull(faciRetrieval1.ReturnValue)

        faciService.Rollback()

        Dim faciRetrieval2 = faciService.GetFacilityByID(migrationResult.ReturnValue.FacilityID)
        Assert.IsNull(faciRetrieval2.ReturnValue)
    End Sub

所以,对于我的问题...

当测试进行到“faciRetrieval1”步骤时,它会在那里停留大约 30-60 秒,然后再继续。我不知道为什么会发生这种情况。如果我在 SSMS 内的事务中运行相同的查询,它会立即发生。有人有什么想法吗?数据库是 SQL Server 2008 SP1(R2?)。

I'm developing a piece of a system that basically migrates data from one set of tables to another set. Everything works fine, but I've decided to employ transactions instead of just failing on things that are partially completed. (That is, if some exception occurs, I want to rollback instead of having partial data migrated.)

I have a service (in the 3-tier architecture way, not web) which begins a transaction on the data access layer. The data context is shared in the data access class which contains many methods. Those methods use various LINQ-to-SQL techniques to update/insert/delete. All the LINQ-to-SQL "selects" are within CompiledQueries.

The "BeginTransaction" method starts a transaction like this:

Public Sub BeginTransaction() Implements ITransactionalQueriesBase.BeginTransaction
    Me.Context.Connection.Open()
    Me.Context.Transaction = Context.Connection.BeginTransaction()
    IsInTransaction = True
End Sub

Basically, I have written a test which starts a transaction, inserts into a table, and then attempts to retrieve the value that was just inserted, all during the transaction. I did this because I wanted to assert that the insert method actually tries to insert. Then, during the test I would rollback, then test to ensure that the newly inserted value is not actually committed to the table. The test looks something like this:

<TestMethod()>
    Public Sub FacilityService_Can_Rollback_A_Transaction()
        faciService.BeginTransaction()

        Dim devApp = UnitTestHelper.CreateDevelopmentApplication(devService.GetDevelopmentType("NEWFACI").ID, 1, 1, 1, 1)

        Dim devInsertRes = devService.InsertDevelopmentApplication(devApp)

        Assert.IsTrue(devInsertRes.ReturnValue > 0)

        For Each dir1 In devInsertRes.Messages
            Assert.Fail(dir1)
        Next

        Dim migrationResult = faciService.ProcessNewFacilityDevelopment(devInsertRes.ReturnValue)
        Assert.IsTrue(migrationResult.ReturnValue.InsertResult)

        Dim faciRetrieval1 = faciService.GetFacilityByID(migrationResult.ReturnValue.FacilityID)
        Assert.IsNotNull(faciRetrieval1.ReturnValue)

        faciService.Rollback()

        Dim faciRetrieval2 = faciService.GetFacilityByID(migrationResult.ReturnValue.FacilityID)
        Assert.IsNull(faciRetrieval2.ReturnValue)
    End Sub

So, to my problem...

When the test gets to the "faciRetrieval1" step, it stays there for about 30-60 seconds before moving on. I'm not sure why this is happening. If I run the same queries in a transaction within SSMS it happens instantly. Does anyone have any ideas? The database is a SQL Server 2008 SP1 (R2?).

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

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

发布评论

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

评论(1

宣告ˉ结束 2024-11-16 09:35:30

我发现,如果您有一个使用事务的数据上下文,则任何其他数据上下文似乎都无法从相同类型的另一个上下文中进行选择。

我最终通过在事务发生时在每个选择/更新/删除中使用相同的上下文来修复它。

I figured out that if you have a data context using a transaction, any other data context appears to not be able to select from another context of the same type.

I ended up fixing it by using the same context throughout every select/update/delete while a transaction was happening.

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