Question

I'm a little confused around connection pools. I'm using Hibernate on top of a MySQL database. Hibernate's connection pool strategy is determined by c3p0.

What's the relationship between Hibernate's connection pool size vs MySQL's?

My code running Hibernate can be scaled to multiple instances on AWS (so n # of instances each with Hibernate connection pool size of m). All instances however talk to a single RDS MySQL instance, which itself has a connection pool size q.

Does this mean if there are n*m active connections and n*m>q, there will be connections that will have to wait in MySQL's queue?

Thanks!

Was it helpful?

Solution

Your question asks about "Hibernate's connection pool size vs MySQL's". The important distinction to keep in mind is:

  • A connection pool is an application concept, not a database one. The connection pool used by your application (i.e., Hibernate) is simply a way of speeding up the communication to the database. Since establishing each connection is relatively slow, it's more efficient to pre-establish a bunch of connections and then let your application use them as needed.

  • On the other hand, a database doesn't pool connections. Instead, it allows clients to establish connections upon request up to a max limit (e.g., the max_connections parameter in MySQL). If a client asks for a connection and the database has already hit the max limit, then a connection error will occur.

So to answer your question, if you configure your application connection pools to try to pre-establish more connections than the database will allow, you will get a "Too many connections" error from MySQL. This could be avoided by either raising the MySQL configuration parameter or by adjusting your c3p0 max pool size per instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top