From 20c3c58821a708541b5d06a517675cd82c6e9d69 Mon Sep 17 00:00:00 2001 From: Mahdi Azarboon <21277296+azarboon@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:20:21 +0800 Subject: [PATCH 1/3] Update advanced-performance-topics.md --- .../core/performance/advanced-performance-topics.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/entity-framework/core/performance/advanced-performance-topics.md b/entity-framework/core/performance/advanced-performance-topics.md index 4ce3e30638..fff34d7e22 100644 --- a/entity-framework/core/performance/advanced-performance-topics.md +++ b/entity-framework/core/performance/advanced-performance-topics.md @@ -77,6 +77,12 @@ The full source code for this sample is available [here](https://github.com/dotn > [!NOTE] > Although EF Core takes care of resetting internal state for `DbContext` and its related services, it generally does not reset state in the underlying database driver, which is outside of EF. For example, if you manually open and use a `DbConnection` or otherwise manipulate ADO.NET state, it's up to you to restore that state before returning the context instance to the pool, e.g. by closing the connection. Failure to do so may cause state to get leaked across unrelated requests. +### Connection Pooling Considerations + +Connection pooling in Entity Framework is managed at the client level through ADO.NET and works consistently across all SQL Server-based environments, including Azure SQL Database, Azure SQL Managed Instance, and SQL Server (on-premises or in virtual machines). While pooling mechanisms are consistent, service-specific factors may impact pooling efficiency. Entity Framework relies on ADO.NET for database interactions, including connection pooling, which reuses existing database connections to minimize the overhead of repeatedly opening and closing connections. + +When Entity Framework Core executes queries or saves changes, it uses an ADO.NET provider, such as `System.Data.SqlClient` or `Microsoft.Data.SqlClient`, to communicate with the database. ADO.NET transparently implements connection pooling, which is enabled by default and reuses database connections for efficiency. Any pooling configurations, such as maximum or minimum pool sizes, must be set at the ADO.NET level through connection string parameters. Each DbContext instance in EF Core requests a connection from the ADO.NET pool when interacting with the database and returns it to the pool upon disposal, ensuring efficient resource utilization. + ## Compiled queries When EF receives a LINQ query tree for execution, it must first "compile" that tree, e.g. produce SQL from it. Because this task is a heavy process, EF caches queries by the query tree shape, so that queries with the same structure reuse internally-cached compilation outputs. This caching ensures that executing the same LINQ query multiple times is very fast, even if parameter values differ. From 8287afa106cffa6a8e10e5de6f90b09874374fcd Mon Sep 17 00:00:00 2001 From: Mahdi Azarboon <21277296+azarboon@users.noreply.github.com> Date: Fri, 6 Dec 2024 19:17:21 +0800 Subject: [PATCH 2/3] Update advanced-performance-topics.md Tweaked it according to feedback --- .../core/performance/advanced-performance-topics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entity-framework/core/performance/advanced-performance-topics.md b/entity-framework/core/performance/advanced-performance-topics.md index fff34d7e22..258407fea0 100644 --- a/entity-framework/core/performance/advanced-performance-topics.md +++ b/entity-framework/core/performance/advanced-performance-topics.md @@ -79,7 +79,7 @@ The full source code for this sample is available [here](https://github.com/dotn ### Connection Pooling Considerations -Connection pooling in Entity Framework is managed at the client level through ADO.NET and works consistently across all SQL Server-based environments, including Azure SQL Database, Azure SQL Managed Instance, and SQL Server (on-premises or in virtual machines). While pooling mechanisms are consistent, service-specific factors may impact pooling efficiency. Entity Framework relies on ADO.NET for database interactions, including connection pooling, which reuses existing database connections to minimize the overhead of repeatedly opening and closing connections. +Entity Framework does not implement connection pooling itself but relies on ADO.NET or other database providers 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 consistent across databases supported by EF, such as SQL Server, Azure SQL Database, PostgreSQL, and others. While the pooling mechanisms themselves are uniform, factors specific to the database or environment, such as resource limits or service configurations, may affect pooling efficiency. When Entity Framework Core executes queries or saves changes, it uses an ADO.NET provider, such as `System.Data.SqlClient` or `Microsoft.Data.SqlClient`, to communicate with the database. ADO.NET transparently implements connection pooling, which is enabled by default and reuses database connections for efficiency. Any pooling configurations, such as maximum or minimum pool sizes, must be set at the ADO.NET level through connection string parameters. Each DbContext instance in EF Core requests a connection from the ADO.NET pool when interacting with the database and returns it to the pool upon disposal, ensuring efficient resource utilization. From 1426388c636f595f6ae5fb3dd91fce27e4f9d499 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 6 Dec 2024 15:57:26 +0100 Subject: [PATCH 3/3] Proposed tweaks --- .../core/performance/advanced-performance-topics.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entity-framework/core/performance/advanced-performance-topics.md b/entity-framework/core/performance/advanced-performance-topics.md index 258407fea0..25c589f599 100644 --- a/entity-framework/core/performance/advanced-performance-topics.md +++ b/entity-framework/core/performance/advanced-performance-topics.md @@ -79,9 +79,9 @@ The full source code for this sample is available [here](https://github.com/dotn ### Connection Pooling Considerations -Entity Framework does not implement connection pooling itself but relies on ADO.NET or other database providers 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 consistent across databases supported by EF, such as SQL Server, Azure SQL Database, PostgreSQL, and others. While the pooling mechanisms themselves are uniform, factors specific to the database or environment, such as resource limits or service configurations, may affect pooling efficiency. +With most databases, a long-lived connection is required for performing database operations, and such connections can be expensive to open and close. 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. -When Entity Framework Core executes queries or saves changes, it uses an ADO.NET provider, such as `System.Data.SqlClient` or `Microsoft.Data.SqlClient`, to communicate with the database. ADO.NET transparently implements connection pooling, which is enabled by default and reuses database connections for efficiency. Any pooling configurations, such as maximum or minimum pool sizes, must be set at the ADO.NET level through connection string parameters. Each DbContext instance in EF Core requests a connection from the ADO.NET pool when interacting with the database and returns it to the pool upon disposal, ensuring efficient resource utilization. +Connection pooling is completely orthogonal to EF's `DbContext` pooling, which is described above: while the low-level database driver pools database connections (to avoid the overhead of opening/closing connections), EF can pool context instances (to avoid context memory allocation and initialization overheads). 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. ## Compiled queries