dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.54k stars 4.54k forks source link

Consider a better design for ADO.NET connection pooling #24856

Open roji opened 6 years ago

roji commented 6 years ago

In today's ADO.NET, connection pooling is purely an internal concern of ADO providers - no pooling API is exposed to the user in any way. It seems to be an unwritten contract that providers are supposed to pool by default, and disable pooling if the Pooling=false connection string parameter is used. The following is a (too lengthy) analysis of possible directions to get the conversation started.

Disadvantages of the current model

Option 1: An ADO.NET user-facing pool API

The connection string lookup imposed by the internal pooling mechanism could be mitigating by adding a simple, user-facing API. User code would look something like this:

var factory = DbProviderFactories.GetFactory(providerName);
var connFactory = factory.GetConnectionFactory(connectionString);
using (var conn = connFactory.GetConnection()) { ... }

Regarding the naming, instead of GetConnectionFactory():

Option 2: A complete pooling API (not just user facing)

The above only provides a user-facing abstraction which allows avoiding the lookup (and possibly opens up possibility for composing connection provider), but the pool itself is still implemented by each provider. We could go further and provide a more complete abstraction that allows writing connection pools. This would allow pooling implementations which aren't connected to any specific provider (and which could be used with any provider). Users would be able to select a 3rd-party pooling implementation which fits their specific application needs, rather than being locked into a single, provider-implemented pool.

This mainly has to do with defining standard public APIs for communication between the ADO provider and the pool in use. For example, when a connection is returned to the pool its state may need to be reset in a provider-specific, the DbConnection would have to expose that API. There may be other things as well.

If we go down this route, we could also optionally provide a provider-independent, highly efficient connection pool that could be used instead of the providers' ones.

Comparison with JDBC

JDBC has had competing pool implementations for a very long time, which can be used with any JDBC database provider. A nice inspiration is https://brettwooldridge.github.io/HikariCP/, which claims to be the highest-performing pool implementation. The JDBC API includes several abstractions for manaing pooling (ConnectionPoolDataSource, PooledConnection), although more research is needed to understand exactly how the pooling model operates there.

Things to keep in mind

/cc @anpete @ajcvickers @divega @davidfowl

Edit by @roji 15/3: Added "things to keep in mind" with some new points.

divega commented 6 years ago

System.Data Triage: This looks like a good idea, although we are not going to implement it right away. Re option 2, we should consider providing an implicit connection pool that implements know best practices, e.g. to avoid locking and yield good performance, so that ADO.NET providers can reuse it. However that would probably either require defining a public type for the underlying connection object or having the pool be general purpose and not an ADO.NET thing.

Moving to Future for now.

Grauenwolf commented 5 years ago

If we go down this route, we could also optionally provide a provider-independent, highly efficient connection pool that could be used instead of the providers' ones.

That sounds reasonable.

But I would like to point out a huge advantage of System.Data over JDBC is that we don't have to think about connection pooling. Things just work regardless of which database provider we use.

So compared to the other things on the list this is a very low priority item.

roji commented 5 years ago

@Grauenwolf there's one more option that's not really listed above - provide a generic pool implementation suited to database connections, but without exposing it to the user. Providers would then be able to use that implementation instead of their current internal implementations, without any impact on how the user interacts with connections or with the provider (it would be a purely an internal implementation detail). This would still provide many of the advantages listed above - a single, highly-tuned and tested reusable implementation.

But I would like to point out a huge advantage of System.Data over JDBC is that we don't have to think about connection pooling.

Having said that, I've worked with JDBC in the past, and I can't say I understand why not thinking about connection pooling can be considered a "huge advantage"... Dealing with the pool seems to be a very minor detail that's taken care of in 1-2 lines of code... Can you explain what's bothersome in the JDBC approach?

Things just work regardless of which database provider we use.

If you mean that you don't need to concern yourself with pooling, then that's true. However, precisely since every provider has its own pool implementation, things don't just work the same everywhere, as each provider has implementation differences (as well as tuning options and various knobs). Externalizing the pool would provide a consistent pooling experience regardless of provider.

bricelam commented 5 years ago

The internal System.Data.ProviderBase namespace contains common pooling logic shared by Odbc, OleDb & SqlClient. It might be worth taking a look at.

roji commented 5 years ago

Will do, thanks for the pointer @bricelam!

ghost commented 10 months ago

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

cincuranet commented 10 months ago

Pinging.