Linq2Db not disposing off sql connections and results in exhausting the pool size
See original GitHub issueWe are using this as a reference to configure Linq2Db in our .Net Core application and the following is the code to configure it.
public static IServiceCollection ConfigureLinqToDb<IDbContext, DbContext>(
this IServiceCollection services,
IConfiguration configuration,
IWebHostEnvironment environment,
bool enableDetailedLogging = false)
where IDbContext : class
where DbContext : class, IDbContext, IDataContext
{
ConnectionStringSettings linq2DbConnStringSettings = new ConnectionStringSettings();
configuration.GetSection("Linq2DbConnectionSettings").Bind((object) linq2DbConnStringSettings);
if (enableDetailedLogging)
services.AddLinqToDbContext<DbContext>((Action<IServiceProvider, LinqToDbConnectionOptionsBuilder>) ((provider, options) =>
{
options.UseSqlServer(linq2DbConnStringSettings.ConnectionString).UseDefaultLogging(provider);
}), ServiceLifetime.Scoped);
else
services.AddLinqToDbContext<DbContext>((Action<IServiceProvider, LinqToDbConnectionOptionsBuilder>) ((provider, options) =>
{
options.UseSqlServer(linq2DbConnStringSettings.ConnectionString);
}), ServiceLifetime.Scoped);
services.AddScoped<IDbContext, DbContext>();
return services;
}
And following are our sample IDatabaseContext and DatabaseContext classes that we are passing to the above extension method which eventually gets registered with the IOC Container.
public interface IDatabaseContext
{
IQueryable<MyClient> Clients { get; }
}
public class DatabaseContext : DataConnection, IDatabaseContext
{
public DatabaseContext(LinqToDbConnectionOptions<DatabaseContext> options) : base(options) { }
public virtual IQueryable<MyClient> Clients => GetTable<MyClient>();
}
We then inject IDatabaseContext wherever we want to use it in our code. Everything seems to be working fine unless the number of concurrent users suddenly increases. Which results in a swift increase in the number of active connections, which makes the connection pool exhausted. At one time for 300 users of our application, there were more than 700 active database connections. Please note that this happens mostly with Identity Server. This exhaustion of the connection pool results in users’ requests getting timed out and the application becomes unresponsive.
What could we possibly be doing wrong? What are the suggestions to improve this?
Environment details
linq2db.AspNet version: 3.2.3 linq2db.SqlServer version: 3.2.3 Database Server: SQL Server Database Provider: Microsoft OLE DB Operating system: Linux .NET Core version: 3.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Yep, check this thread for more details https://github.com/dotnet/SqlClient/issues/25
@MaceWindu Thank you. I will go through this.