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

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.

Related

Difference between connection, instances, and sessions in SQL

What is the difference between Connection, instances, and sessions in the SQL server? I tried to find the differences but one and others are interrelated.
Connection: It is the number of instances connected to the database.
Sessions: A session run queries.
Instance: It is a copy of the sqlservr.exe executable that runs as an operating system.
Any explanation with examples would be helpful.
A connection refers to the layer-4 (TCP or Named Pipes or Shared Memory or etc...) connection between a client and the server.
A session is the "logical" unit of a client connection and uniquely identifies a client. A session can have multiple active commands and is the entity which "holds" locks on an object. Sessions are normally 1:1 with connections (the exception that comes to mind is the Context Connection.)
An instance is the SQL Server process running on a server which provides the SQL Interface to the databases. It frequently listens on TCP ports to accept connections from clients to create new sessions.

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).

Where is the cache of SQL Server Browser service for resolving Instance name's port number?

From here:
When SQL Server clients request SQL Server resources, the client network library sends a UDP message to the server using port 1434. SQL Server Browser responds with the TCP/IP port or named pipe of the requested instance.
Apparently when UDL or SSMS used for connecting remotely to SQL Server instance name, the query for resolving the instance name's port number store's somewhere in client machine.
I tested this by two client machines. When the 1434 UDP port was open first machine could connect to SQL Server instance name. Then I closed the port and tried again with that machine. The first client still could connect without the port being open. Then I tried with second machine but it couldn't connect.
I just wondering how and where this caching takes place?
Client APIs like SqlClient use connection pooling by default to avoid the overhead of name resolution, physical network connection, and authentication every time a connection is opened. When the initial connection is closed, the connection is added to a connection pool where it can be reused the next time another connection with the same attributes is opened. The client API in that case simply retrieves and unused connection from the pool avoiding the significant overhead of establishing the physical connection.
With a named instance, connection pooling also avoids the need to query the SQL Server Browser service every time a connection is opened so this explains your observations. I suspect if you exit and re-launch the application after blocking UDP port 1434, the SQL connection for the named instance will fail due to the failed SQL Server Browser data gram query during the initial connection open.

SQL Server connection handling, connection pooling, lifetime and leaked connections

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.

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.

Resources