Connection Pool overview
What is connection Pool ?
Connection pooling is the technique used to increase the performance of the application when an application makes a multiple connection requests.
Connection pooling takes care of managing connections(prepare,open and close)
How connection pool works ?
Connection Pool API is responsible for opening N connections and leave them ready for our application.
Our application will just ask for the pool to get a new connection, it will use it and then deliver it back to the Pool.
If a connection is stale, the pooling mechanism would then close it and re-open a new one.
This way we can use the connections in a more better way, as we no need to wait for the connection to be established during the actual execution of our code and we don’t have to worry about stale connections
Why we need to use Connection pool ?
We know that, opening a database connection is an expensive operation, so pooling keeps the connections active.
When a connection is requested by an application, one of the active connection is given to the application rather than opening new one.
This will increase the performance drastically when there are multiple requests to the database in our application.
Connection Pool in Hibernate
Hibernate has provided a default connection pooling mechanism but it is not that much scalable to use in production.
So most of the applications uses the third party connection pool providers for better performance.
Some of the third party connection pool providers are DBCP,C3P0, BoneCP and Proxool etc.
Most widely used connection pool provider is c3p0.
Hibernate automatically identifies which connection pool provider to use based on the properties configured in the config file.
However we can define the connection provider with the < hibernate.connection.provider_class > property as below in the config file
- //Config property for specifying connection pool
- <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property >
//Config property for specifying connection pool <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property >
Note :If we do not configure a connection pool, the default is used.
Pingback: Best Database Practices - Single Connection vs Connection Pool