Connection pool error

Hi all!

I got an error today in my ASP.NET application:

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.

So I found out it has to do with the number of connections open (and not closed). I reviewed all the code, and when Im opening a database as follows:

Dim conString = ConfigurationManager.ConnectionStrings("MyDataBase").ConnectionString
Dim con As New SqlConnection(conString)
Dim SqlString as string = "select username from users"
Dim cmd As New SqlCommand(SqlString, con)
Dim RS As SqlDataReader = cmd.ExecuteReader

Im closing the connection and the datareader as follows too:


I browsed all 469 files on my project to check Im closing the connections, but when I run the following script on SQL:


I still see a lot of open connections with the value AWAITING COMMAND on the CMD field.

In my connection string, I increased the value of "Max Pool Size" to 500 and the error went away, but I am affraid the error will return.

I also read I can set pooling = false  on the connection string, but that would bring performance issues.

So my questions are basically two:

1) Why do I still see the "AWAITING CONNECTION" records? (I see between 50 and 400 constantly). Does this mean I am still having some connections Im not closing?

2) Will pooling = false be a bad move?

3) Suggestions? :)


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pvg1975Author Commented:
On question 1 I mean "AWAITING COMMAND", not connection. Sorry :P
Pooling wouldn't hurt much. I will try to give more information on it when i will be little free.
Meanwhile you can also check the max number of connnection setting in sql server
select @@max_connections

you can set the recycle time for the pool in the IIS server for the application pool you are using.
pvg1975Author Commented:
Hi TempDBA!

I run select @@max_connections and I got this value: 32767

The recycle time in minutes in IIS is 1740. Should I change that value?

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Alfred A.Commented:
Is there a chance that an exception might be occurring between con.Open() and con.Close()?  If you are not handling exceptions occurring between the two, then there is a possibility that connections are not being closed.  Trying using a Using statement to make sure your connections are closed and disposed regardless of whether errors occur or not.
Dim conString = ConfigurationManager.ConnectionStrings("MyDataBase").ConnectionString
Dim SqlString as string = "select username from users"
Using con As New SqlConnection(conString)
    Using cmd As New SqlCommand(SqlString, con)
        ''Using CommandBehavior is optional considering code is in Using Statement
        Dim RS As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) 
    End Using
End Using

Open in new window

pvg1975Author Commented:
Hi Alfred,

I was using USING until I read on Microsoft forums that sometimes USING does not close the connection. Right now its too late to modify 469 files...

I was thinking on setting pooling = false in the connection string, but I read somewhere that its a bad move.

Now, when I set Max Pool Size = 500, does that mean that the IIS pool will handle a maximum of 500 connections, the session for the particular user will use 500 connections, the entire application will use 500 connections?

I still dont understand the implications of using Max Pool Size

Alfred A.Commented:
The only issue I can think of regarding using large Max Pool Size is that it will consume more memory.  

You could try to use Performance Monitor to watch .NET DataProvider for SqlServer/NumberOfPooledConnections counter to determine the max pool size value.

Also, if you don't want to use Using Statement, then you could try to force the connection to close by putting it in the finally clause in a try/catch/finally setup.

Check this out for reference as well:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Take a look at this link, you can specify the timeour of the connection, which will close the connection after a period of time


Public Sub CreateSqlConnection()
    Dim myConnection As New SqlConnection()
    myConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30"
End Sub 'CreateSqlConnection

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.