• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 589
  • Last Modified:

Error :Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have...

Hi ,
In my Asp.net web application when i try to access a web-service after sending my credentials i get the 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. "

But when  i try to access the same web service url on my localhost , while debugging my local application in VS , it runs perfectly and  provides me with xml response.
I am not able to fix the problem   I have tried following things :

1) Increased the connection timeout on my web server.
2) Set the pool size to maximum .

But still it persists.

Please help , I am confused about the location of problem , my web server or the server from where i am accessing the web service .

1 Solution
See increasing connection pool size only will not help. Check if you are opening connection explicitly need to close it as well.

Check in code whether you are closing or not the connection object.

Please let me know whether this works or not.

Shriniwas Wani
It seems that your web service is trying to access an SQl Server database.
Check whether SqlConnection object is properly closed using SqlConnection.Close() method.
Also check in the SQL server instance, how many connections are open from web service connection string using sp_who procedure.
You can also use the performance monitor console to check the connection pool of the SQL Server instance.  Try the following link.

Also see this article if you are closing connections properly.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

vikasbapatAuthor Commented:
                        shrinivasmw , Thanks for ur answer .
Yes i am closing the connection .Please see code below thats causing the error.

 string projecturl = "www.mywebservice.com/userid/password"
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(projecturl);
        req.Method = "GET";
        req.ContentType = ".xml";
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        string backstr = sr.ReadToEnd();
 sr.Close();//                     This is where i close connection

Hi Vikas,

You must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.

So modify ur code :
       res.Close();//  close stream obj first then
 sr.Close();//                     This is where i close connection

I belive this will help. Please let me know if it works or not.

Shriniwas Wani
vikasbapatAuthor Commented:

I modified my code as per you said . But its still same ..after 3-4 qattempts it cpnnects to web service .

My question however is : Why it throws this exception when i try to access a 3rd party web service & not to my own database ???
Thanks for ur help .. I really appreciate it
Hi Vikas,
This is all memory/connection object related issue.

I will suggest you to go through this article on MSDN.

It will help. Let me know if it works.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now