【问题标题】:Set PENDING lock to EF core SqlLite将 PENDING 锁设置为 EF 核心 Sqlite
【发布时间】:2018-12-05 15:45:09
【问题描述】:

我正在使用 EF Core 和 SqlLite 开发 ASP .Net Core Web-api 应用程序。如果有很多选择查询,更新查询会很慢。当查询数量增加时,我收到“数据库已锁定”错误。 我认为问题是“Writer starvation”,所以我想在 DBContext 将数据保存到 DB 时为我的更新设置 PENDING 锁。

如何使用 EF 内核设置 PENDING 锁?还有其他解决办法吗?

更新:

  1. 即使查询不多,有时也会发生数据库锁定

  2. 包:

    <PackageReference Include="Abp.AspNetCore.OData" Version="3.8.2" /> 
    <PackageReference Include="Abp.AspNetCore.SignalR" Version="3.7.2" />
    <PackageReference Include="Abp.AspNetCore" Version="3.8.2" />
    <PackageReference Include="jquery.datatables" Version="1.10.15" />
    <PackageReference Include="MailKit" Version="2.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.OData" Version="7.0.1" />        
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.3" />
    <PackageReference Include="BuildBundlerMinifier" Version="2.8.391" />    
    <PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
    <PackageReference Include="NLog.MailKit" Version="3.0.0" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.7.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.3" />
    <PackageReference Include="Castle.Core" Version="4.3.1" />    
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.1.1" PrivateAssets="All" />
    <PackageReference Include="Abp.Dapper" Version="3.8.2" />
    <PackageReference Include="Abp.EntityFrameworkCore" Version="3.8.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
    

【问题讨论】:

    标签: c# sqlite asp.net-core transactionscope ef-core-2.1


    【解决方案1】:

    我认为 Entity Framework 没有提供任何方法来设置更新的挂起锁,但您可以使用 Database.Begin API 来开始、提交和回滚事务。

    您可以在更新任何事物时启动事务,并在此时间间隔内完成更新后提交事务,没有人能够更新数据库中的任何数据。

    using (var context = new BloggingContext())
    {
        using (var transaction = context.Database.BeginTransaction())
        {
            try
            {
                //Did your work here
    
                //During this time it will help as a pending lock for other updates
               transaction.Commit();
            }
            catch (Exception)
            {
                // TODO: Handle failure
            }
        }
    }
    

    希望它能解决您的问题。

    更新

    请注意,只有那些使用BloggingContext() 的另一个实例执行的查询才会被设置为挂起锁,此行为不适用于用于启动事务的BloggingContext() 实例。

    【讨论】:

    • 它是否适用于 sqlLite?我将日志记录添加到上下文中,似乎 EF 不支持 sqlLite 事务
    • @Timur Lemeshko 它应该可以工作。我没有看到 SQLite 的这种限制。 docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations
    • 如果不支持,则应该抛出NotSupportedException
    • 我没有任何例外。日志中有“提交事务”,但日志中确实没有 START TRANSACTION 和 COMMIT 并且看起来事务确实不起作用,但我不舒尔
    • BEGIN 和 COMMIT 语句由 ADO.NET 提供程序发送,EF Core 不记录。
    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2021-08-13
    • 2019-03-06
    相关资源
    最近更新 更多