SQL Server connection handling, connection pooling, lifetime and leaked connections - sql-server

I am trying to identify SQL connection leaks in my application. After some operations, when my application is idle (user not doing any activity), I see 7 connections with my database in the result set returned by sp_who2. The status for all connections is Sleeping and Command value for all of them is AWAITING COMMAND.
I am using connection pooling but Connection Lifetime is not specified in the connection string. This means that it's default value 0 will be used if I am right. Connection Lifetime having value zero means SQL server should not close connection ever, right?
I keep my application idle for some time (15-20 minutes) and then I see that sp_who2 does NOT show any connection with my database. I am wondering why I get this result when Connection Lifetime is zero. Does SQL Server terminates unused connection after some time regardless of the Connection Lifetime value?
How can I identify which connection is open due to leakage and which is hanging around there due to connection pooling?
My application supports SQL Server 2008, 2014 and 2016. It's ASP.NEt application.

Connection Lifetime having value zero means SQL server should not close connection ever, right?
No. The rules used to purge the pool are not well documented AFAIK, but they can be closed by the pooler. Refer https://msdn.microsoft.com/en-us/library/8xx3tyca%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396.
I don't think there is a reliable/documented method to identify pooled connections from within the database engine.

Related

How to create a connection pool of connections to the SQL Server Database, ensuring any connections are kept Open

For my project that is being developed in .NET Core 2.2, i'm trying to implement a Connection Pool of SQL Server Database connections. So that whenever the business entity will need to connect any of the database, it will just pull the connections from Connection pool and use. Also need to keep the connections in the connection pool alive. All the databases are in the Azure.
Can you please guide me or provide me an example, how to implement the connection pool in C# code?
Whether the Connection pool can be implemented using any standard library?
Specify the Min Pool Size= connection string keyword with System.Data.SqlClient or Microsoft.Data.SqlClient to indicate in initial number of pooled connections. These physical connections will remain open in the pool when not in use.
Connections are normally acquired on demand and closed automatically when not used for several minutes so this setting is generally used only for specialized cases. See SQL Server Connection Pooling (ADO.NET).

Sql resets my pooling connections after idle time

I'm using a client pc and i'm connecting via tcp/ip into a local database server. For some reason, after my connection will be idle for a while, then Sql keep alive is checking my tcp connection if it's still active. If the time when keep alive is checking my connection and tcp/ip traffic is high or other reasons, then it resets my connection inside pooling. So the result is that if my connection will be reset ,the first connection which client pc will try to run, it will get exception "TCP Provider, error: 0 - No connection could be made because the target machine actively refused it
" or " Server closed the connection" or something like this. This happens cause client pc will try to connect in a session which there is not exist inside sql pooling. I have set min pool size =10. But i still have the same problem.
after keep alive time
Here is my connection string
Data Source = xxx.xxx.xxx.xxx;Min Pool Size=20; Initial Catalog = WiOrder; user id = xxxx; password = 1234;Connection Timeout=5;
SQL Server does not terminate connections. What you are seeing is being done by the .Net driver on your client machine and is completely normal and expected behavior.
Please see:
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling#removing-connections

Connection Failure after a long idle time with SQL Server 2016

I have an old VB6 application that establishes a connection to the SQL Server and uses it throughout the lifetime of the application.
We recently upgraded the back end to SQL Server 2016 (from 2005) and noticed an odd issue. If the user leaves the application idle for several hours, and then tries to do anything having to do with the database, they get run-time error -2147219450 (80040806) or Connection Failure. The application does check the ADODB.Connection.State and it always reports that the connection is open. We are seeing this error pretty consistently on multiple, pretty diverse (in terms of OS versions) workstations.
I haven't seen anything on Google relating to this. Can someone shed some light on this issue?
I was getting this issue with this driver only when connecting to sqlserver instance on my local machine. Seemed like there was a bug in the shared memory client protocol. Switching to TCP for local connections (Turning off shared mem and named pipes) solved the problem.
the error i was getting was
Microsoft OLE DB Provider for SQL Server "Connection failure" SQL State: 08S01

Getting the connection pool error even when setting the connection string property Pooling to false

I am having some connection pool issues in my sharepoint application. Every time that my application tries to fetch some data from an SQL Server 2008 R2, I got this exception:
"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 know that I am probably having connections leak problems, but I have checked all parts of my code that I am accessing the database, and all of them are implemented with the using() pattern. My SQL Server version is 2008 R2 Express, so I don't have the Profiler Tool to see how many connections my application is actually creating.
I have tried disable the pooling setting the Pooling=false; in my connection string, but I've got the same error. Also, I have tried increase the connection timeout and the max pool size with no success.
Inspecting the User Connections on my SQL Server instance before my application get failed using the perfmon, I have got that the number of connections is not even close to 100 (the default max pool size).
One important information is that if I run this application in another computer here in my office, it works perfectly.
Obs: I am using entity context to access the database and this application is not published, I am just run it locally with vs2013.
If you guys know some good way to inspect the connections behavior of my application or have a idea of what could be happening in my development environment, please share with me.
Thanks.

IIS SQL Server: Connection Pool: Why are new connections made instead of reusing existing connections?

During app loading a single user will add 10 connections to the pool. Then if I load another user I will have a total of 20 connections.
Why are new connections being added to the pool instead of reusing connections? I can see that that there are connections available that haven't been used in minutes yet it still opens new connections.
Its the same connection string
I ran SQL Sever Profiler I can see sp_reset_connection being called after every call.
Any help would be appreciated.
If the connections are coming from a different machine, the connections can't be pooled. A connection required both endpoints to be a connection. If you are using connection pooling correctly, applications instantiate a connection (from the pool), use the connection, then drop it as soon as exchange is complete.
If you a writing a single-threaded desktop app, another common and simple strategy is to open a connection, and just leave that connection open as long as the application is running.
You can control how many connections are created, etc. see MS article for more details related to connection pooling.
IIRC, connection pools are not shared unless the connection string is identical either.

Resources