8000 Fix DbContext pooling by sfmskywalker · Pull Request #6531 · elsa-workflows/elsa-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix DbContext pooling #6531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
<NoWarn>$(NoWarn);IL2026;IL2046;IL2057;IL206 8000 7;IL2070;IL2072;IL2075;IL2087;IL2091</NoWarn>
</PropertyGroup>
<PropertyGroup>
<ElsaStudioVersion>3.3.0-rc4</ElsaStudioVersion>
<ElsaStudioVersion>3.4.0-rc1</ElsaStudioVersion>
</PropertyGroup>
</Project>
418 changes: 166 additions & 252 deletions Directory.Packages.props

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions src/apps/Elsa.Server.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

// ReSharper disable RedundantAssignment
const PersistenceProvider persistenceProvider = PersistenceProvider.EntityFrameworkCore;
const bool useDbContextPooling = true;
const bool useHangfire = false;
const bool useQuartz = true;
const bool useMassTransit = true;
Expand Down Expand Up @@ -236,6 +237,8 @@
else
identity.UseEntityFrameworkCore(ef =>
{
ef.UseContextPooling = useDbContextPooling;

if (sqlDatabaseProvider == SqlDatabaseProvider.SqlServer)
ef.UseSqlServer(sqlServerConnectionString);
else if (sqlDatabaseProvider == SqlDatabaseProvider.PostgreSql)
Expand Down Expand Up @@ -286,6 +289,7 @@
else
management.UseEntityFrameworkCore(ef =>
{
ef.UseContextPooling = useDbContextPooling;
if (sqlDatabaseProvider == SqlDatabaseProvider.SqlServer)
ef.UseSqlServer(sqlServerConnectionString);
else if (sqlDatabaseProvider == SqlDatabaseProvider.PostgreSql)
Expand Down Expand Up @@ -336,6 +340,7 @@
else
runtime.UseEntityFrameworkCore(ef =>
{
ef.UseContextPooling = useDbContextPooling;
if (sqlDatabaseProvider == SqlDatabaseProvider.SqlServer)
{
//ef.UseSqlServer(sqlServerConnectionString, new ElsaDbContextOptions);
Expand Down Expand Up @@ -495,6 +500,7 @@
{
alterations.UseEntityFrameworkCore(ef =>
{
ef.UseContextPooling = useDbContextPooling;
if (sqlDatabaseProvider == SqlDatabaseProvider.SqlServer)
ef.UseSqlServer(sqlServerConnectionString);
else if (sqlDatabaseProvider == SqlDatabaseProvider.PostgreSql)
Expand Down Expand Up @@ -634,15 +640,20 @@
management.ConfigureOptions(options => configuration.GetSection("Secrets:Management").Bind(options));
if (sqlDatabaseProvider == SqlDatabaseProvider.SqlServer)
management.UseEntityFrameworkCore(ef =>
ef.UseSqlServer(sqlServerConnectionString)
);
{
ef.UseContextPooling = useDbContextPooling;
ef.UseSqlServer(sqlServerConnectionString);
});
else if (sqlDatabaseProvider == SqlDatabaseProvider.PostgreSql)
management.UseEntityFrameworkCore(ef =>
ef.UsePostgreSql(postgresConnectionString)
);
{
ef.UseContextPooling = useDbContextPooling;
ef.UsePostgreSql(postgresConnectionString);
});
else
management.UseEntityFrameworkCore(ef =>
{
ef.UseContextPooling = useDbContextPooling;
ef.UseSqlite(sp => sp.GetSqliteConnectionString());
});
})
Expand Down Expand Up @@ -695,6 +706,7 @@
{
management.UseEntityFrameworkCore(ef =>
{
ef.UseContextPooling = useDbContextPooling;
if (sqlDatabaseProvider == SqlDatabaseProvider.Sqlite) ef.UseSqlite(sqliteConnectionString);
if (sqlDatabaseProvider == SqlDatabaseProvider.SqlServer) ef.UseSqlServer(sqlServerConnectionString);
if (sqlDatabaseProvider == SqlDatabaseProvider.PostgreSql) ef.UsePostgreSql(postgresConnectionString);
Expand Down
14 changes: 4 additions & 10 deletions src/modules/Elsa.EntityFrameworkCore.Common/ElsaDbContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ public override async Task<int> SaveChangesAsync(CancellationToken cancellationT
return await base.SaveChangesAsync(cancellationToken);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
#if NET9_0_OR_GREATER
optionsBuilder.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning));
#endif
}

/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand All @@ -78,7 +70,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

additionalConfigurations?.Invoke(modelBuilder);

var entityTypeHandlers = ServiceProvider.GetServices<IEntityModelCreatingHandler>().ToList();
using var scope = ServiceProvider.CreateScope();
var entityTypeHandlers = scope.ServiceProvider.GetServices<IEntityModelCreatingHandler>().ToList();

foreach (var entityType in modelBuilder.Model.GetEntityTypes().ToList())
{
Expand All @@ -89,7 +82,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

private async Task OnBeforeSavingAsync(CancellationToken cancellationToken)
{
var handlers = ServiceProvider.GetServices<IEntitySavingHandler>().ToList();
using var scope = ServiceProvider.CreateScope();
var handlers = scope.ServiceProvider.GetServices<IEntitySavingHandler>().ToList();
foreach (var entry in ChangeTracker.Entries().Where(IsModifiedEntity))
{
foreach (var handler in handlers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;

namespace Elsa.EntityFrameworkCore;
Expand All @@ -24,12 +25,12 @@ protected PersistenceFeatureBase(IModule module) : base(module)
/// <summary>
/// Gets or sets a value indicating whether to use context pooling.
/// </summary>
public bool UseContextPooling { get; set; }
public virtual bool UseContextPooling { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to run migrations.
/// </summary>
public bool RunMigrations { get; set; } = true;
public virtual bool RunMigrations { get; set; } = true;

/// <summary>
/// Gets or sets the lifetime of the <see cref="IDbContextFactory{TContext}"/>. Defaults to <see cref="ServiceLifetime.Singleton"/>.
Expand All @@ -50,13 +51,19 @@ public override void ConfigureHostedServices()
/// <inheritdoc />
public override void Apply()
{
if(DbContextOptionsBuilder == null)
if (DbContextOptionsBuilder == null)
throw new InvalidOperationException("The DbContextOptionsBuilder must be configured.");


Action<IServiceProvider, DbContextOptionsBuilder> setup = (sp, opts) =>
{
opts.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning));
DbContextOptionsBuilder(sp, opts);
};

if (UseContextPooling)
Services.AddPooledDbContextFactory<TDbContext>(DbContextOptionsBuilder);
Services.AddPooledDbContextFactory<TDbContext>(setup);
else
Services.AddDbContextFactory<TDbContext>(DbContextOptionsBuilder, DbContextFactoryLifetime);
Services.AddDbContextFactory<TDbContext>(setup, DbContextFactoryLifetime);
}

protected virtual void ConfigureMigrations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,26 @@ public override Action<IServiceProvider, DbContextOptionsBuilder> DbContextOptio
Module.Configure<EFCoreWorkflowInstancePersistenceFeature>(x => x.DbContextOptionsBuilder = value);
}
}

public override bool UseContextPooling
{
get => base.UseContextPooling;
set
{
base.UseContextPooling = value;
Module.Configure<EFCoreWorkflowDefinitionPersistenceFeature>(x => x.UseContextPooling = value);
Module.Configure<EFCoreWorkflowInstancePersistenceFeature>(x => x.UseContextPooling = value);
}
}

public override bool RunMigrations
{
get => base.RunMigrations;
set
{
base.RunMigrations = value;
Module.Configure<EFCoreWorkflowDefinitionPersistenceFeature>(x => x.RunMigrations = value);
Module.Configure<EFCoreWorkflowInstancePersistenceFeature>(x => x.RunMigrations = value);
}
}
}
Loading
0