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

3

47

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.

Smegma answered 2/2, 2011 at 22:20 Comment(0)
R
69

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.

Raulrausch answered 3/2, 2011 at 8:8 Comment(3)
@ jpkrohling, Excellent explanation :-)Kershner
@jpkrohling, so is it ok if I let the Hibernate default connection pool for production?Leporine
I have the same question as @Ommadawn. It's a nice explanation but not very helpful. For example if I Tomcat or Wildfly - How can I use their connection pool?Integration
C
6

When you are dealing with a standalone application there are a couple of pooling managers which have not been maintained by Hibernate. Hibernate never favored one explicit. Over the years many have come and faded again. It is really hard to judge in the end which are best. It is good to check and compare by yourself the projects and how active these are still.

Here are some recent (2017) pooling recommendations for standalone applications in alphabetic order:

C3P0 http://www.mchange.com/projects/c3p0/

Hikari https://github.com/brettwooldridge/HikariCP

Vibur http://www.vibur.org/

Copulative answered 12/10, 2017 at 22:36 Comment(2)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Oriental
Hi Police man, my answer is simply a help for people dropping in here. It is quite hard to tell which connection pooling is the appropriate one. So I try to be as neutral as possible. Of course you might downvote but this is not Wikipedia here, it is opinion based for sure but no sailer will die simply by having an answer here with some links. So please try to be not as rude.Copulative
B
-5

The default connection pool in hibernate is c3p0 named after the star wars character. But hibernate supports also proxool and used to also advertise apache dbcp. For a while DBCP was dormant and fell out of grace.

C3P0 is actually used in production in many projects. Although it is sometimes found to behave poorly at peak time. There are several alternatives. Like for instance the new connection pool included in Tomcat 7. I haven't tested it yet though, but heard some positive feedbacks about it.

Belamy answered 2/2, 2011 at 22:46 Comment(4)
Thanks for your answer, is the 'community documentation' out of date then?Smegma
No, I don't think so. Seems the problem has more to do with the community's inertia. Since 3.3 (2008) C3P0 is uncoupled from hibernate (provider class C3P0ConnectionProvider moved out of core jar) and it looks like the developers try to make the use of c3p0 less straightforward but that the community always homes back to its old habits.Belamy
Do you have any reference supporting your affirmation that the default connection pool in Hibernate is c3p0?Deceive
I don't think default connection pool in hibernate is c3p0Babysitter

© 2022 - 2024 — McMap. All rights reserved.