We're running a busy ASP.NET Core Web API backed by SQL Server, and lately the logs show intermittent deadlocks tied to database connections. I suspect it's related to how we're managing DbContext instances, especially since we enabled connection pooling at the server level.
Our current setup registers AddDbContext with a scoped lifetime in Program.cs. Under load, connections seem to be held longer than expected, and I've noticed some requests timing out while waiting for a free connection from the pool.
Is there a recommended way to tune the pool size or configure DbContext options to reduce contention? Should we be looking at EnableRetryOnFailure, or is this more about ensuring we're not accidentally keeping connections open across await boundaries?
First, separate two problems that often get called “connection deadlocks.” DbContext pooling reuses EF Core context instances; ADO.NET connection pooling reuses database connections. They are separate pools, and configuring one does not increase the capacity of the other.
Register a pooled context with a sensible retained-context limit, then size the database connection pool separately if measurements show it is needed:
The context pool size is not a limit on how many requests can use EF Core at once. Contexts beyond that number can still be created; they simply are not retained for reuse. The database driver's connection pool has its own settings, such as SQL Server's
Max Pool Sizein the connection string. Increasing either pool blindly can hide a bottleneck rather than fix it.Check how connections and contexts are used
DbContextis not thread-safe. Await each EF operation before starting another, or use a separate context for genuinely independent parallel work. This avoids overlapping parallel EF Core operations on the same instance.OpenConnection, ensure it callsCloseConnectionor disposes the connection reliably, including on exceptions.Pooling also means a context instance can be reused by a later request. Avoid storing request-specific mutable values on it, such as a tenant ID, unless you reset them safely every time the context is leased. Pooled contexts should not depend on scoped services injected into their constructor; that state can outlive the request. This is a common source of pooled context state leaking between requests.
When diagnosing production symptoms, look at database connection-pool wait times and active connections alongside database deadlock reports. If requests are waiting for available connections, find where connections are held too long and compare concurrent demand with the driver's pool limit. If the database logs deadlock victims, investigate the transactions and queries instead. DbContext pooling can reduce context allocation overhead, but it cannot resolve either problem by itself.
The Core Problem
When you enable pooling on your DbContext, you're handing out physical connections from a limited pool rather than creating new ones per request. That sounds efficient, but it introduces a subtle risk: if a request holds a connection longer than expected while waiting on an async operation or a lock, other requests can stack up, waiting for the same limited resource. In high-traffic production environments, this is often what manifests as connection-related deadlocks or timeouts.What Pooling Actually Changes
By default, Entity Framework creates a new connection per DbContext instance and closes it when the context is disposed. With pooling, the underlying physical connection is returned to the pool instead of being torn down. This reduces the overhead of opening and closing connections, but it also means your application's connection usage becomes a shared resource. The pool size is typically capped (often around 100 by default in many providers), so exceeding that threshold causes requests to queue.Practical Configuration Steps
The exact settings depend on your provider, but the general pattern is consistent. You need to register your DbContext with pooling enabled and tune the pool size to match your concurrency profile.If you're on an older project using
DbContextOptionsBuilder, the equivalent looks like this:Why Deadlocks Happen with Pooling
Deadlocks in this context usually aren't SQL deadlocks in the traditional sense. They're more often resource contention: multiple requests hold connections from the pool, and one of them waits on a lock or async signal that another request owns. Because pooled connections are reused, a "stuck" request effectively blocks the physical connection for the duration of its hold. If your application logic nests multiple async calls withoutConfigureAwait(false)where appropriate, or if you're synchronizing across threads with locks while a connection is active, you amplify the risk.Mitigation Strategies Beyond Pooling
Performance Countersor provider-specific metrics can show you how often the pool is exhausted. If you see sustained high utilization, the pool size is likely too small for your load.When Not to Pool
Pooling isn't a silver bullet. If your application performs long-running batch operations, bulk imports, or operations that hold connections for minutes, pooling can actually hurt by starving shorter requests. In those cases, consider disabling pooling or using a dedicated connection for those workloads.Bottom Line
Properly configuring DbContext pooling is less about flipping a switch and more about understanding your connection lifecycle. Enable it, size the pool to your concurrency needs, keep your DbContext usage brief, and watch for contention. That combination is what keeps production stable.