java - maxIdleTimeExcessConnections 与 c3p0 中的 maxIdleTime 有何不同?

标签 java jdbc connection-pooling c3p0

我想配置我的 c3p0 连接池,以便至少有 2 个连接始终处于 Activity 状态,最多 5 个,并且前 2 个连接之后的空闲连接将在合理的时间内(例如一小时)过期。

这一切听起来很简单,只是文档似乎暗示函数 maxIdleTimemaxIdleTimeExcessConnections 之间没有区别,这让我感到困惑。

Basic pool configuration状态:

Within the range between minPoolSize and maxPoolSize, the number of Connections in a pool varies according to usage patterns. The number of Connections increases whenever a Connection is requested by a user, no Connections are available, and the pool has not yet reached maxPoolSize in the number of Connections managed. Since Connection acquisition is very slow, it is almost always useful to increase the number of Connections eagerly, in batches, rather than forcing each client to wait for a new Connection to provoke a single acquisition when the load is increasing. acquireIncrement determines how many Connections a c3p0 pool will attempt to acquire when the pool has run out of Connections. (Regardless of acquireIncrement, the pool will never allow maxPoolSize to be exceeded.)

minPoolSize用法:

Minimum number of Connections a pool will maintain at any given time.

好的,太好了。以及配置连接时间:

maxIdleTimeExcessConnections is about minimizing the number of Connections held by c3p0 pools when the pool is not under load. By default, c3p0 pools grow under load, but only shrink if Connections fail a Connection test or are expired away via the parameters described above. Some users want their pools to quickly release unnecessary Connections after a spike in usage that forces a large pool size. You can achieve this by setting maxIdleTimeExcessConnections to a value much shorter than maxIdleTime, forcing Connections beyond your set minimum size to be released if they sit idle for more than a short period of time.

因此,这意味着 minPoolSize 仅在与 maxIdleTimeExcessConnections 结合使用时才重要,否则,它将被完全忽略。

证实 maxIdleTime 的文档没有提及 minPoolSize:

Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.

并且 maxIdleTimeExcessConnections 是有道理的:

Number of seconds that Connections in excess of minPoolSize should be permitted to remain idle in the pool before being culled. Intended for applications that wish to aggressively minimize the number of open Connections, shrinking the pool back towards minPoolSize if, following a spike, the load level diminishes and Connections acquired are no longer needed. If maxIdleTime is set, maxIdleTimeExcessConnections should be smaller if the parameter is to have any effect. Zero means no enforcement, excess Connections are not idled out.

我觉得很奇怪的是,minPoolSize 这个基本功能只有在与我认为更高级的功能一起使用时才有意义。这一切都正确吗?

最佳答案

多么仔细和律师的阅读!

但是不,这是不正确的。

连接终止的方式有多种。正如您引用的:

c3p0 pools...shrink if Connections fail a Connection test or are expired away via the parameters described above.

“上述参数”包括 maxConnectionAgemaxIdleTimemaxIdleTimeExcessConnections。连接也可以从池中删除,因为它们在空闲时未通过连接测试(请参阅idleConnectionTestPeriod),因为它们在 checkin 或 checkout 时未通过测试(testConnectionOnCheckintestConnectionOnCheckout),或者因为在客户端使用过程中因异常触发的测试失败。

无论池如何缩小,minPoolSize 都很重要,因为如果池缩小到 minPoolSize 以下,则被破坏的连接将被替换,直到 minPoolSize 已恢复。

maxIdleTimeExcessConnections 的独特之处在于,它的行为直接取决于池相对于 minPoolSize 的大小。所有其他参数和测试都只是发挥作用。如果他们的事情碰巧使池低于 minPoolSize,那么 c3p0 会自动将池恢复到 minPoolSize。但 maxIdleTimeExcessConnections 是不同的。仅当池大于 minPoolSize 时,它才有效。

正如您所说,maxIdleTimeExcessConnections 是一项高级功能。大多数用户从不也不需要使用它。添加它是因为一些用户希望积极强制池收缩回 minPoolSize,但是使用非常短的无条件 maxIdleTime 来实现这一点会导致连接不必要的流失,因为即使在 minPoolSize 中,连接也是如此code> 池不断过期和替换。设置较长或不存在的 maxIdleTime,同时设置较短的 maxIdleTimeExcessConnections 会产生快速、积极收缩的预期结果,一旦池达到 minPoolSize,就不会搅动连接>.

但即使没有设置 maxIdleTimeExcessConnectionsminPoolSize 也非常重要。连接确实会被销毁并从池中删除,并且 minPoolSize 确定一个阈值,低于该阈值,即使没有客户端负载引起池扩展,被破坏的连接也会被自动替换。

我希望这是有道理的!

关于java - maxIdleTimeExcessConnections 与 c3p0 中的 maxIdleTime 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22695281/

相关文章:

java - 23/12/2013 是用 MM/dd/yyyy 格式映射的,为什么,为什么不是 ParseException

java - 如果我未安装 Axis,则不明白它是如何工作的?

通过 jdbc 进行 sql 查询

python - 线程和 SQLAlchemy session

java - 嵌入式Jetty 8和无XML的JNDI MySQL连接池的配置

java - ByteArrayOutputStream 转换期间添加的特殊字符

java - JBoss 6 从 WEB-INF/lib of war 解压 jar

java - JDBC 驱动程序在空 ResultSet 上抛出 "ResultSet Closed"异常

hibernate - 在 Spring Boot 中,同时使用 c3p0 与 jdbcTemplate 和 Hibernate

java - 如何检查天气客户端是否在 java 中使用 PoolingHttpClientConnectionManager