java Tomcat Datasource 配置 Connection timeout 和 Max Active to Idle connection ratio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16076441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:49:38  来源:igfitidea点击:

Tomcat Datasource configuration Connection timeout and Max Active to Idle connection ratio

javadatabasetomcatjdbc

提问by Learn More

I am having a web application which is load balanced on four servers. These three servers connect to common database with max connections setup to 600.

我有一个 Web 应用程序,它在四台服务器上进行负载平衡。这三个服务器连接到公共数据库,最大连接数设置为 600。

My Current database pool configuration in tomcat is as follows:

我在tomcat中的当前数据库池配置如下:

<Resource name="jdbc/AppDB"
                          auth="Container"
                          type="javax.sql.DataSource"
                          maxActive="100"
                          maxIdle="30"
                          maxWait="10000"
                          removeAbandoned ="true"
                          removeAbandonedTimeout="300"
                          testOnBorrow="true"
                          validationQuery="select 1"
                          logAbandoned ="true"
                          username="username"
                          password="password"
                          driverClassName="com.mysql.jdbc.Driver"
                          url="dbconnectionurl"
                 />

But this configuration gives Exception: Connection Timeout : Waiting for Idle Object.

但是这个配置给出了异常: Connection Timeout : Waiting for Idle Object.

I have monitored database server, my effective utilization of connections is 350 only.

我监控了数据库服务器,我的有效连接利用率只有 350 个。

If I calculate it backwards, I have total 400 connections used actively. This gives me 200 extra database connections available from DB.

如果我向后计算,我总共有 400 个连接被积极使用。这为我提供了 200 个来自 DB 的额外数据库连接。

Hence, I am not able to understand why this Exception is coming.

因此,我无法理解为什么会出现此异常。

Can anybody suggest a better configuration ? What should be the ideal ratio of maxActiveand maxIdle?

有人可以建议更好的配置吗?maxActive和的理想比例应该是maxIdle多少?

UPDATE:

更新:

I found a related link: There are not enough idle connections in Tomcat JDBC pool

我找到了一个相关链接: Tomcat JDBC pool 中没有足够的空闲连接

But I can not risk setting maxIdle or MaxActive to -1(INFINITE) because I have multiple instances running, and it might make uneven distribution of connections. This may cause one instance to perform nicely and other may fail due to lack of connections.

但是我不能冒险将 maxIdle 或 MaxActive 设置为 -1(INFINITE),因为我有多个实例在运行,这可能会导致连接分布不均。这可能会导致一个实例运行良好,而其他实例可能由于缺乏连接而失败。

回答by skirsch

First off, maxIdledoesn't have anything to do with your problem, as this only defines how many connections are kept in the pool that are not actively used - excess connections will be dropped. To illustrate this: imagine that at t180 connections are in use; at t2only 30 connections are still in use, meaning 50 connections are put back into the pool. The maxIdlesetting of 30 now causes the pool to close 20 of the 50 idle connections.
Now if at t350 connections are in use again, the "idle pool" would contain only 10 connections. The number of connections in the idle pool is not actively increased!

首先,maxIdle与您的问题没有任何关系,因为这仅定义了池中保留了多少未主动使用的连接 - 多余的连接将被丢弃。为了说明这一点:假设在t1 处有80 个连接正在使用;在t2 时,只有 30 个连接仍在使用,这意味着 50 个连接被放回池中。maxIdle现在 30的设置会导致池关闭 50 个空闲连接中的 20 个。
现在,如果在t3再次使用 50 个连接,“空闲池”将仅包含 10 个连接。空闲池中的连接数没有主动增加!

To make that clear: maxActiveis the maximum number of connections the pool provides. maxIdledoes not add another bunch of connections on top of that; it is just a means to keep around a number of connections for the base load, speeding up the connection usage (if maxIdlewould be 0, each connection returned to the pool would be closed, thwarting the idea of a connection pool).

明确地说:maxActive是池提供的最大连接数。maxIdle不会在其上添加另一串连接;它只是一种为基本负载保留一些连接的方法,加快了连接的使用(如果maxIdle是 0,则返回到池的每个连接都将被关闭,从而阻碍了连接池的想法)。

To suggest a proper ratio for your case is kind of hard, as it depends on the load curve your application has to face. But for the problem you are facing (the pool does not return an idle, i.e. "free" connection), it wouldn't matter if you'd set it to -1, as the pool is simply exhausted.

为您的案例建议合适的比率有点困难,因为这取决于您的应用程序必须面对的负载曲线。但是对于您面临的问题(池不返回空闲的,即“空闲”连接),如果您将其设置为 并不重要-1,因为池只是耗尽了。

So to fix your problem, you either need to increase maxActiveor you need to find ways to use the connections more efficiently.

因此,要解决您的问题,您要么需要增加maxActive连接,要么需要找到更有效地使用连接的方法。