Reputation: 24301
What is the best way to setup your pool with respect to:-
I believe this is an agnostic question, but comments about "features" of particular databases/languages are welcome. For example, it might be slower or more expensive to connect on some databases than others.
To clarify, I do not intend to write a pool from scratch, this question is more about how to configure an existing library that does pooling.
Upvotes: 10
Views: 2690
Reputation: 881653
Here is the rationale I used for a recent implementation.
Have two sorts of connections in your connection pool. The first is ready, meaning open but not in use by a client. The second is active, meaning in use by a client.
Have your connection pooling maintain a small number of ready connections, minimum of N and maximum of M. N can be adjusted depending on the peak speed at which your clients request connections. If the number of ready connections ever drops to zero, you need a bigger N. If the number is consistently high (say above 10), you need a lower N.
When a client wants a connection, give them one of the ready ones (making it active), then immediately open a new one if there's now less than N ready (but don't make the client wait for this to complete, or you'll lose the advantage of pooling). This ensures there will always be at least N ready connections. If none are ready when the client wants one, they will have to wait around while you create a new one.
When the client finishes with an active connection, return it to the ready state if there's less than M ready connections. Otherwise close it. This prevents you from having more than M ready connections.
Periodically recycle the ready connections to prevent stale connections. If there's more than N ready connections, just close the oldest connection. Otherwise close it and re-open another.
This has the advantage of having enough ready and youthful connections available in your connection pool without overloading the server.
Upvotes: 3
Reputation: 22348
I wrote a connection pool for the database in Java when it was just a design pattern and not a common library. Now I use the one built into Tomcat.
I used a thread to monitor several aspects of the pool and several parameters to control its behavior...
This served me very well for a couple of years. The highest I ever saw the pool was 151 connections during a wild peek. Usually the pool was at about a dozen during heavy usage and idled down to the minimum three in the early morning hours.
I used Oracle's JDBC thin drivers and connected to an Oracle database.
Upvotes: 6
Reputation: 4270
I’m not sure what the context in which you are using your connections but I can share what seems to work for me.
I use SQL server as my back end and use a combination of caching with it to get better performance. My practice is to keep the connection open only if I actually need it and to not pool connections so that they clean up right away and I can see in SQL Activity monitor exactly what is active and what’s not. Each connection takes up memory so it’s nice to keep it to a dull roar when they aren’t needed.
Before I answer the connection open and close question let me say that caching is really important. Getting an object out of the cache is going to save you a ton of time. In some of my asp.net apps when caching is on in dev I have found that I can hardly measure the latency whereas with a DB call it might take anywhere from 15ms to 45ms to complete the call and this isn’t even considering other latency factors or load. The other method that I use is a good object structure for my data so that I only make a DB update if something changes. I’ve implemented some methods on my object o make sure that I’ve doing as little IO as possible.
That being said we all know that we need to access and write to our DB at some point so I follow two principles:
Keep the doors and windows closed to save on energy. An open connection in one place means that it’s not available in another (or the memory and other resources are more limited). We have turned pooling off because it has resulted in better performance for us.
I do as much in batch or at once as I can when the connection is open. This is a bit more complicated so let me explain.
My two cents:
Cache your data! Cache your data! Cache your data! Do as little DB access as possible when you can’t cache and then cache your data!
Upvotes: 2
Reputation: 5596
I agree with matt b that we should not reinvent the wheel.
However using Commons DBCP is arguable based on the answers of this and this questions. There are better alternatives mentioned there like c3po or proxool.
Or you may use rdbms dependent connection pooling mechanism.
Upvotes: 2
Reputation: 5159
Jakarta Commons DBCP already does all the stuff you listed:
You can fine tune your pool by playing with the minimum number of connections, the max number of connections to be created, and the timeout. A longer timeout will allow you to have a lower limit of connections, while a shorter timeout will probably required a larger number. This depends heavily on what your application does and how it uses the connections.
Upvotes: 2
Reputation: 139931
Why re-invent the wheel?
Someone has already probably solved the problem, and better.
If you're in the Java world, you can use Commons DBCP.
Upvotes: 1