Browse Source

Don't use RETURNING clause with EFCore

The RETURNING clause helps with performance and is now default of EFCore. However, EFCore cannot automatically perform retry when the table was locked/busy. Disable it as a workaround for the locking issues of very huge databases.
gnattu 4 months ago
parent
commit
b0e853070b
1 changed files with 21 additions and 0 deletions
  1. 21 0
      Jellyfin.Server.Implementations/JellyfinDbContext.cs

+ 21 - 0
Jellyfin.Server.Implementations/JellyfinDbContext.cs

@@ -4,6 +4,8 @@ using Jellyfin.Data.Entities;
 using Jellyfin.Data.Entities.Security;
 using Jellyfin.Data.Interfaces;
 using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+using Microsoft.EntityFrameworkCore.Metadata.Conventions;
 using Microsoft.Extensions.Logging;
 
 namespace Jellyfin.Server.Implementations;
@@ -271,4 +273,23 @@ public class JellyfinDbContext(DbContextOptions<JellyfinDbContext> options, ILog
         // Configuration for each entity is in its own class inside 'ModelConfiguration'.
         modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDbContext).Assembly);
     }
+
+    /// <inheritdoc/>
+    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
+    {
+        configurationBuilder.Conventions.Add(_ => new DoNotUseReturningClauseConvention());
+    }
+
+    private class DoNotUseReturningClauseConvention : IModelFinalizingConvention
+    {
+        public void ProcessModelFinalizing(
+            IConventionModelBuilder modelBuilder,
+            IConventionContext<IConventionModelBuilder> context)
+        {
+            foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
+            {
+                entityType.UseSqlReturningClause(false);
+            }
+        }
+    }
 }