Solved connection pooling question

Posted on 2011-03-21
Last Modified: 2012-06-27
I'm trying to understand connection pooling as it relates to a multi-threaded application.
If I have 5 threads continuously running and they all create a sql connection, shouldn't I have 5 distinct connections to the db?  Using the sql trace I don't see but 2.

If I up my threads to 10 the connection count doesn't change.  Inside the thread, the connection is opened and closed as it is used.

I understand the connection pooling just returns it back to the pool but I would still think I would have more than 2 connections at any given time. The threads run very quickly but upping the thread count doesn't seem to improve performance.

Question by:rutledgj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 13

Expert Comment

ID: 35184419

Connection pooling is a useful way to reuse connections for multiple requests, rather than paying the overhead of opening and closing a connection for each request. It's done implicitly, but you get one pool per unique connection string. If you're generating connection strings dynamically, make sure the strings are identical each time so pooling occurs.

Hope that helps

Author Comment

ID: 35185549
So are you saying that one connection created can result in multiple connections through connection pooling?
LVL 13

Expert Comment

ID: 35185614
Well, connection pooling use active connections of the pool for consecutive requests, rather than creating a new connection each time.

So if your threads have the same  Connection String, the first one will Create and Open a DB Connection and the second, third, etc will use the same connection instead of create another a new one.
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Author Comment

ID: 35185772
so the second (and subsequent) threads will have to wait until the first thread finishes with the connection until it can use it? The connection it a single object created as a public shared variable.
LVL 13

Expert Comment

ID: 35186044
Ok, the connection pool will work this way when a thread needs a DB Connection:

If any unused connection is available, it returns one.

If all connections are used up, a new connection is created and added to the pool.

If the number of connections reaches the maximum number of connections in the pool, the requests are queued until a connection becomes free for reuse.

So, the second and subsequent threads will have to wait ONLY if the connection created by the First Thread is still in use, if not, they will use it rigth away

Author Comment

ID: 35188757
I guess this is what doesn't make sense in your statement.

If any unused connection is available, it returns one.

If all connections are used up, a new connection is created and added to the pool.

If my application opens one connection for all threads to use and thread #1 is using it, does the connection pool create more connections for the remaining threads or do they have to wait for thread #1 to finish?
LVL 13

Accepted Solution

gamarrojgq earned 250 total points
ID: 35189227
ok, that would depend on the pool maxpoolsize property, if you set this property > 1, if the Thread #1 is using the connection, the connection pool will create another connection for the memaining threads, but if you are not using connection pool or the maxpoolsize = 1, then the other threads will have to wait


Thread 1 Request a DB Connection    
       The connection Pool Creates a New One
Thread 1 Use it and Close it

Thread 2 Request a DB Connection
       The connection Pool reuse the connection Created by Thread1
Thread 2 START using the connection

Thread 3 Request a DB Connectino
       Since the Thread 2 is still using the connection Created by Thread1 it Creates a Second
Thread 3 Use the new Connection an Close It

Thread 2 Close the connection Created by Thread1

And this cycle repeats according to the Threads DB Connections requests


Author Closing Comment

ID: 35189425
Thanks. Just one comment. Isn't connection pooling used by default in Didn't know you had to turn it on.
LVL 13

Expert Comment

ID: 35190133
Yes, it is on by default, but you can turn it off if you want.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

626 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