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

Posted on 2009-12-23
Last Modified: 2012-05-08
Hi ,
In my 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 .

Question by:vikasbapat
    LVL 2

    Expert Comment

    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
    LVL 10

    Expert Comment

    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.

    LVL 8

    Expert Comment

    Also see this article if you are closing connections properly.

    Author Comment

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

     string projecturl = ""
      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

    LVL 2

    Expert Comment

    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

    Author Comment


    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
    LVL 2

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now