Search code examples
hibernateconnection-pooling

Hibernate: What is the connection pool and why is the default one unsuitable for production?


I'm very unfamiliar with Hibernate and have just started working on a web app that uses it with a MySQL database. I notice that the community documentation tutorial states:

The built-in Hibernate connection pool is in no way intended for production use. It lacks several features found on any decent connection pool.

Can someone elaborate on this? What exactly is it missing and what are problems people have with the 'default' one? On googling I found a website here but it doesn't really explain the problems, just what you should be using instead.


Solution

  • What is the connection pool and why is the default one unsuitable for production? Can someone elaborate on this?

    Connection pooling is a technique to open/prepare/close connections. A connection pooling mechanism is a piece of software (component), to which you delegate the function of managing connections. Your application would just ask for a connection, use it, and deliver it back to the pool. The component is responsible for opening N connections and leave them ready for when your application asks. If a connection is stale, the pooling mechanism would then close it and reopen a new one. This represents a better usage of connections, as you don't need to wait for the connection to be established during the actual execution of your code and you don't have to worry about stale connections.

    Hibernate doesn't really ship any real connection pooling mechanism. It provides an internal connection manager, which is very rudimentary. The reason is simple: almost (if not all) Application Servers (like JBoss AS) and Servlet Containers (like Tomcat) provides a connection pooling mechanism by default. Thus, your application don't have to worry about the details about it. It just asks the AS for a connection.

    In my opinion, there are only two cases where you need to worry about connection pooling:

    1. You are dealing with a standalone application (which doesn't run inside a container)
    2. You are really expert in connection pooling and none of the existing suits your needs.

    But in my experience, most people that uses an "external" connection pooling do so for lack of knowledge about connection pooling and lack of knowledge about their container.