Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Entity Framework Core 9 manage database connections?

I have a web application that is using a db connection pool.

In my code I have something like this:

services.AddDbContextPool<MyDbContext>

Then I have each command being controlled by dependency injection:

services.AddScoped<IRequestHandler<GetControlGroupsQuery, IEnumerable<ControlGroupDto>>, GetControlGroupsQueryHandler>();
services.AddScoped<IRequestHandler<UpdatePicklistCommand, CommandResultDto>, UpdatePicklistCommandHandler>();

My understanding is each time I get a request and handle it, I create an instance of the DbContext, but I reuse the connections of the connection pool.

enter image description here

Is my understand correct?

My real problem: once in a while, I get some errors saying I have reached the max pool connections. How do I make sure to release the connections? The request handler function returning is not enough? (they are scoped per request)

like image 731
RagnaRock Avatar asked May 18 '26 19:05

RagnaRock


1 Answers

My understanding is each time I get a request and handle it, I create an instance of the DbContext, but I reuse the connections of the connection pool.

No, AddDbContextPool enables pooling of the context instances itself, not the connections. From the DbContext pooling section of the docs:

A DbContext is generally a light object: creating and disposing one doesn't involve a database operation, and most applications can do so without any noticeable impact on performance. However, each context instance does set up various internal services and objects necessary for performing its duties, and the overhead of continuously doing so may be significant in high-performance scenarios. For these cases, EF Core can pool your context instances: when you dispose your context, EF Core resets its state and stores it in an internal pool; when a new instance is next requested, that pooled instance is returned instead of setting up a new one. Context pooling allows you to pay context setup costs only once at program startup, rather than continuously.

The connection pooling is handled on the lower level:

Note that context pooling is orthogonal to database connection pooling, which is managed at a lower level in the database driver.

And from the Connection Pooling Considerations section:

EF does not implement connection pooling itself, but relies on the underlying database driver (e.g. ADO.NET driver) for managing database connections. Connection pooling is a client-side mechanism that reuses existing database connections to reduce the overhead of opening and closing connections repeatedly. This mechanism is generally consistent across databases supported by EF, such as Azure SQL Database, PostgreSQL, and others., although factors specific to the database or environment, such as resource limits or service configurations, may affect pooling efficiency. Connection pooling is usually enabled by default, and any pooling configuration must be performed at the low-level driver level as documented by that driver; for example, when using ADO.NET, parameters such as minimum or maximum pool sizes are usually configured via the connection string.

And

Regardless of whether a context instance is pooled or not, EF generally opens connections just before each operation (e.g. query), and closes it right afterwards, causing it to be returned to the pool; this is done to avoid keeping connections out of the pool any longer than is necessary.

Also answer for Managing PostgreSQL connection for the best performance can have some useful information.

I get some errors saying I have reached the max pool connections

This can mean different things AFAIK. One of the possibilities is that you have to many concurrent and/or long requests which leads to the connection pool exhaustion. You can increase the connection pool size via the connection string (depended on the database used). Also note that using transactions can come into play here - generally you can't reuse the connection if there is an active transaction associated with it (so check that you have only DB manipulation inside the transaction and do not call other services, storages etc.).

like image 168
Guru Stron Avatar answered May 20 '26 08:05

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!