Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring a context with a DbDataSource builder can defeat connection pooling #29489

Closed
roji opened this issue Nov 6, 2022 · 1 comment
Closed

Comments

@roji
Copy link
Member

roji commented Nov 6, 2022

In EFCore.PG, I've added a UseNpgsql overload that accepts an NpgsqlDataSource (instead of a connection string) - that works well and allows a pre-built data source to be used as the source for connections in EF (see npgsql/efcore.pg#2400).

However, that requires users to separately build a data source before calling UseNpgsql, with NpgsqlDataSourceBuilder. Ideally, extension APIs on DbContextOptionsBuilder would allow configuring the NpgsqlDataSourceBuilder, and EF would get the data source from that. In addition, for various type mapping scenarios (type mapping plugins, native enum support...), it should be possible for a DbContextOptionsBuilder to affect the data source, setting up type mapping at the ADO.NET layer (e.g. the EF-layer NodaTime plugin would add the ADO.NET-layer NodaTime plugin to NpgsqlDataSourceBuilder).

This is currently problematic; since OnConfiguring is called for every context instance, a data source would be built for each context instanc, defeating pooling. This doesn't occur when the options are provided externally via the constructor (e.g. DI, context pooling).

A possible way forward would be allow throwing when OnConfiguring is used with APIs that need to modify NpgsqlDataSourceBuilder, forcing the user to switch to externally-provided options instead. Or maybe something else.

@roji
Copy link
Member Author

roji commented Nov 17, 2022

Team discussion: as a specific solution for the Npgsql problem, it could count the number of NpgsqlDataSources instantiated via the NpgsqlDataSourceBuilder API, and warn/throw when that crosses a certain threshold. This is similar to the service provider error EF itself has.

Opened #29597 for a wider discussion on OnConfiguring and configuration patterns.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant