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)
con.Open()
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:

RS.close()
con.close()

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

SELECT SPID,
 STATUS,
 PROGRAM_NAME,
 LOGINAME=RTRIM(LOGINAME),
 HOSTNAME,
 CMD
 FROM MASTER.DBO.SYSPROCESSES
 WHERE DB_NAME(DBID) = 'MYTABLE' AND DBID != 0

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? :)

Thanks!

Paula.
pvg1975Asked:
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
0
TempDBACommented:
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
go

you can set the recycle time for the pool in the IIS server for the application pool you are using.
0
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?

Thanks.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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)
        con.Open()
        ''Using CommandBehavior is optional considering code is in Using Statement
        Dim RS As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) 
        ..
        ..
        RS.close()
    End Using
End Using

Open in new window

0
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

Thanks!
0
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:

http://www.codeproject.com/KB/dotnet/ADONET_ConnectionPooling.aspx
0

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
santhimurthydCommented:
Take a look at this link, you can specify the timeour of the connection, which will close the connection after a period of time

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(VS.71).aspx

Sample

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

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.