I use Entity Framework 6.4.4 in my multi-threads server application to connect to a SQL Server database on another server on the internet.
Before using DbContext, I open the connection by calling MyDbContext.Database.Connection.Open()
. Multiple threads may try to open the connection at the same time. But sometimes I get this exception of type InvalidOperationException with Message: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
I am sure I close all the connections after being used, and no connection remains open for more than about 2 seconds. And also, I am sure there are not many simultaneous connections.
The SQL Server can support about 1500 connections at the same time (I have tested), but I think the problem is with opening a connection not having many opened connections.
I tested my application on a server with a CPU of 40 logical processors. It works fine. But when I move my application to a server with 4 logical processors, it works correctly for a while, but it can not open even one single connection after a period. I limited the number of threads tries to open the connections simultaneously to even 3. Still, it didn't help, and I get that exception continuously. CPU usage is consistently below 50%, and there is free memory.
Asked
Active
Viewed 605 times
0

Dadkhah
- 99
- 4
-
You probably have code to close the connections and are trying to close the connections, but most likely some logic error is making you not to close them. Hard to know the reason without any code, but you can use the SQL Server profiler (included in the SQL Server Management Studio) and do some tracing, checking the opening and closing of the connections. You can also do some reading on [SQL Server connection pools](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling) – Cleptus Apr 28 '21 at 06:59
-
IDbConnections are IDisposable and should generally only be used in `using (...) {...}` blocks. It sounds like you have code that opens connections but doesn't close them, causing the Connection Pool to run out of connections. If the EF server is Windows based you can activate and monitor the [ADO.NET performance counters](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/performance-counters) to watch for connections not getting released back to the connection pool. – AlwaysLearning Apr 28 '21 at 09:09
-
Does this answer your question? [C# Data Connections Best Practice?](https://stackoverflow.com/questions/17552829/c-sharp-data-connections-best-practice) You must dispose connections with `using` blocks immediately after you have finished with them, do not cache them – Charlieface Apr 28 '21 at 09:52
-
Thanks @Cleptus . I found my problem. The threads become lcoked on a lock statement and all waiting for a new DB connection. But due to reaching max pool connection size, no new connection could be made. My problem is solved just by setting a high number for the max pool size in the connection string. – Dadkhah Apr 29 '21 at 07:01