question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Linq2Db not disposing off sql connections and results in exhausting the pool size

See original GitHub issue

We 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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Jan 25, 2021

Yep, check this thread for more details https://github.com/dotnet/SqlClient/issues/25

0reactions
muradkhateeb78commented, Jan 25, 2021

@MaceWindu Thank you. I will go through this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing Data Connection
When the pool size limit is reached then your application would fail to obtain a new connection. To avoid connection leaks you should...
Read more >
Why does this code exhaust the connection pool so quickly?
This may have occurred because all pooled connections were in use and max pool size was reached. ' This always happens on the...
Read more >
ADO.NET Database Pool Exhaustion issue
I have recently worked on an issue that was caused by ADO.NET database pool exhaustion. This is one of the most common reasons...
Read more >
Cost of Increasing max pool size in SQL Server
If connections are indeed properly closed and disposed when not actively used (i.e. immediately returned to the pool) and you have capacity ...
Read more >
linq2db
LINQ to DB is the fastest LINQ database access library offering a simple, light, fast, and type-safe layer between your POCO objects and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found