简体   繁体   English

C3P0连接池与MySQL连接池

[英]C3P0 Connection Pools vs MySQL Connection Pools

I'm a little confused around connection pools. 我对连接池有点困惑。 I'm using Hibernate on top of a MySQL database. 我在MySQL数据库之上使用Hibernate。 Hibernate's connection pool strategy is determined by c3p0. Hibernate的连接池策略由c3p0决定。

What's the relationship between Hibernate's connection pool size vs MySQL's? Hibernate的连接池大小与MySQL的关系是什么?

My code running Hibernate can be scaled to multiple instances on AWS (so n # of instances each with Hibernate connection pool size of m). 我运行Hibernate的代码可以扩展到AWS上的多个实例(因此每个Hibernate连接池大小为m的n个实例)。 All instances however talk to a single RDS MySQL instance, which itself has a connection pool size q. 但是,所有实例都与单个RDS MySQL实例进行通信,该实例本身具有连接池大小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? 这是否意味着如果有n * m个活动连接且n * m> q,那么将有必须在MySQL队列中等待的连接?

Thanks! 谢谢!

Your question asks about "Hibernate's connection pool size vs MySQL's". 你的问题是关于“Hibernate的连接池大小与MySQL的关系”。 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 (ie, Hibernate) is simply a way of speeding up the communication to the database. 应用程序使用的连接池(即Hibernate)只是加速与数据库通信的一种方式。 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 (eg, the max_connections parameter in MySQL). 相反,它允许客户端根据请求建立连接,最大限制(例如,MySQL中的max_connections参数)。 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. 因此,要回答您的问题,如果您将应用程序连接池配置为尝试预先建立比数据库允许的更多连接,则MySQL将收到“Too many connections”错误。 This could be avoided by either raising the MySQL configuration parameter or by adjusting your c3p0 max pool size per instance. 这可以通过提升MySQL配置参数或调整每个实例的c3p0最大池大小来避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM