dotnet / dotnet/efcore

Is AddDbContextPool supposed to release contexts from the pool?

Open
#35,855 3 comments 0 reactions 0 assignees View on GitHub
area-diagnostics customer-reported needs-design
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### Question

I have a large context model, so I'm experimenting with context pooling.

My current setup is:

```
service.AddDbContextPool((provider, optionsBuilder) =>
{
AppDbContextOptionsFactory.Get(optionsBuilder);
});
```

When I run my app, everything seems to work ok, except the `active_dbcontexts` seems to indicate the pooled dbcontexts are created but never released. Is this the expected behaviour for this?

![Image](https://github.com/user-attachments/assets/2bb1d59b-05ec-499f-b7b9-2da62878d35f)

From debugging with EFCore symbols, it appears that `AddDbContextPool` uses a `ScopedDbContextFactory` that does not allow the lease to be released because `IsStandalone` is set to false.

### EF Core version

9.0.0

### Database provider

SqlServer

### Target framework

.Net 9

### Operating system

Windows 10

### IDE

vs 2022

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.