Example & Tutorial understanding programming in easy ways.

What is Connection pools used for in Hibernate?

Connection Pooling: When we open a connection to a database is generally much more expensive than executing an SQL statement. A connection pool is used to minimize the number of connections opened between application and database. It serves as a librarian, checking out connections to application code as needed. Much like a library every application code needed to be strict about returning to the pool when complete, for it it does not do so, application will run out of available connection.


Starving a Pool: When using connection pooling, it is important to remember that a chunk of bad code neglets to return connections can be starve the rest of the application, causing it to eventually run out of connections and hang (potentially failing now here near the actual problem). To test for this, set the maximum connections in your pool to a small number ( as low as 1 ), and use tools like p6spy and IronTrack sql (described above) to look for statements that fail to close.


This problem can be avoided by always using a finally block to close connection.


Hibernate supports a variety of connection pooling mechanisms. If an application server, you may wish to use the built in pool ( typically a connection is obtaining using JNDI). If you can't or don't wish to use your application servers built in connection pool, Hibernate supports serveral other connection pool.

Read More →