I understand this post will only appeal to a very small group of developers, however I find it useful. Hibernate is a ORM service for Java. ORM stands for Object/Relational Mapping, meaning you can map your plain old Java objects (POJO) directly to your chosen database tables and columns with ease. Hibernate will even build your Java classes as database tables and columns for you! One of the biggest performance issues for newer developers working with databases is managing connections.
I have done it myself; write a bunch of database code that does all your persistence work for you which is typically quite mundane and can leave your code a little sloppy. Hundreds of lines of quirky SQL and calls to open database connections without closing them properly (as you cut and paste your way through the process).
Hibernate first of all allows you to simplify all of this mess by allowing you to just ‘persist’ your objects (instead of performing your own serialization/un-serialization by manually reading in and writing the values of your instance variables). Hibernate is way to big for me to discuss in this article, but if you are curious about ORM and how it can drastically improve your applications why not take a look at Hibernate.org
The main point to this article is to discuss connection pooling. This can be a grey area for developers; mainly those who come from hack-n-slash development (hacking PHP scripts, building quick and dirty scripts with direct calls to databases like MySQL or PostgreSQL). Normally developers from those backgrounds never really understand just what happens with database’s when you connect (in fact most new developers don’t actually understand the code, they just copy and paste and change the SQL).
Most of the time developers manually open a database connection (either directly or via a database abstraction API or layer). A lot of the time they forget to close the connection after they open it which (can) leave hundreds of orphaned open connections to the database server - clogging up resources and slowing everything right down.
Connection pooling helps streamline all that by opening and closing connections on your behalf. When explaining pooling I always use a ‘boat’ metaphor, this is how I explain it.
Imagine an office (represents your application) a stationary store (represents your database) and cars (that represent a database connection). Your office needs some supplies from a stationary store (or needs to return them). In order to get to the store you need to drive there, it would look a little something like this:

What is supposed to happen is the application (office) needs to open a connection (grab a car) and then drive to the store and pick up some stationary (talk to the database and run a query) and then drive back to the office again to bring the stationary home (read query result). Normally the car would be parked back in the garage and have it’s engine turned off (close the connection). However developers get sloppy when building code and the following scenario unfolds:
the office requests some stationary, a car is fetched from the garage (opens a connection) and then drives to the store. After fetching the stationary the car is then driven back to the office, but instead of parking the car and turning off the engine, the stationary is taken out of the car, and it’s just left outside the office with the doors open and the engine running. The next time the office requests stationary, instead of using the car already running outside, a NEW car is fetched and the process repeated, this keeps on going and going, until you have a hundred cars parked outside the building, all running and waiting. Eventually there will be so many cars blocking the opffice that you won’t be able to get any more cars out (database server refuses any more connections).
So it ends up looking like this:

Eventually the database server will scream “NO MORE!”. The cars represent a connection that hasn’t been closed and put away properly. The Database can only support a certain number of constantly open connections before it will ignore any new ones.
So how does Pooling work? it’s a very simple actually. The data provider (in this case Hibernate) will automatically open a minimum number of connections for you, or in the case of our scenario - it will fetch ten cars and park them outside the office, ready and waiting to go to the store. The ‘Pool’ is the group of cars already running and ready to go.

Now the office/developer never needs to worry about picking up a car and parking it and turning off the engine, All it has to do is put in the request to Hibernate - which in turn will then grab a car from the pool, go and grab what is needed from the store, and put the car back in the pool, ready for another request. By using pooling you can allow multiple cars to be running different requests to and from the store at the same time without having to open and close multiple connections constantly.
This allows your office to grow and grow, allowing more requests to be generated. But what happens if the number of requests (people needing stationary) is greater than the number of available cars in the pool? Well, Hibernate will simply increase the size of the car/connection pool for as many requests as are required. It will also decrease the size of the pool (park the un-used cars and turn off the engine) after a set time period. There is a ‘minimum’ size that is always maintained (always making sure there are x number of cars available) as well as having an upper limit of the pool (to cope with a large surge if required).
Hibernate comes with a great pooling system called C3P0, to set it up, simply add the following line’s to your hibernate.cfg.xml file:
<!– minimum size –>
<property name=”hibernate.c3p0.min_size”>10</property>
<!– maximum size –>
<property name=”hibernate.c3p0.max_size”>40</property>
<!– timeout before connections are closed –>
<property name=”hibernate.c3p0.timeout”>1800</property>
<!– maximum statements that can be made –>
<property name=”hibernate.c3p0.max_statements”>50</property>
There are more options you can include but I won’t add them here, as I said this isn’t a tutorial.
So by using pooling with Hibernate, you can increase the flexibility and the scalability of your database enabled Java applications.
If you want to know more, why not take a look at: HowTo configure the C3P0 connection pool.
Happy hacking.
- Dave
Very clear. Helpful.
Hi Dave,
You wide my vision in connection pooling. It helps me a lot. Marvellous article!
hello DAve
nice artical abt pooling ..bt i have one question. i not use pooling in my application bt some time with using hibernate when my running application idle more than 8hr connection with database is close and application down…
in that case how pooling keep the coonection where the default wait time of mysql to keep coonection alive is only 8 hr after that it close that connection.. so is pooling help me in that scenario…
waiting fr ur feed back
Regard
Noman
MNomansadiq@gmail.com, MNomansadiq@hotmail.com
@Nommi
The problem you’re having is that the connection you have to your mysql server is being left open without being properly closed or managed, you’re getting a timeout, Pooling will fix all this for you - you won’t get time outs.
I have only ever seen timeouts when working with JDBC directly, are you using Hibernate as an ORM layer or JDBC direct?
A quick solution would to have your application make a random request every two hours or so, as long as there is some kind of traffic moving from your app to your database server, the connection won’t timeout. Or you could just increase the timeout on your mysql server, I once set it to two weeks! heh.
Very much Useful artical.
I want some wide range of knowlege about hibernate pooling