java 我可以更改连接的 max-pool-size 吗?是什么决定了我应该将其设置为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/599422/
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
Can I change the max-pool-size for connections, and what determines what I should set it to?
提问by cletus
Encountering the following error with our J2EE application:
我们的 J2EE 应用程序遇到以下错误:
java.sql.SQLException: Error in allocating a connection. Cause: In-use connections equal max-pool-size and expired max-wait-time. Cannot allocate more connections.
java.sql.SQLException:分配连接时出错。原因:使用中的连接等于 max-pool-size 和过期的 max-wait-time。无法分配更多连接。
How do I know how many connections the application is currently using and what should be the optimal connection pool settings for a heavily traffic application? Can I change it, and how can I determine what I should set it to (is it a memory issue, bandwidth, etc.)?
我如何知道应用程序当前正在使用多少个连接,以及对于流量大的应用程序,最佳连接池设置应该是什么?我可以更改它吗,我如何确定我应该将它设置为什么(是否是内存问题、带宽等)?
回答by cletus
How to know how much connection the application is currently using
如何知道应用程序当前使用了多少连接
You don't give enough information to answer that. Most appservers will have some sort of JMX reporting on things like this. Alternatively, depending on the database, you could find the number of currently open connections.
你没有提供足够的信息来回答这个问题。大多数应用服务器都会有某种 JMX 报告这样的事情。或者,根据数据库,您可以找到当前打开的连接数。
what should be the optimal connection pool settings for a heavily traffic applicaiton
流量大的应用程序的最佳连接池设置应该是什么
Higher than what you've got?
比你拥有的更高?
The above of course assumes that you're not mishandling connections. By that I mean if you're using them directly in code you should always use this idiom:
以上当然假设您没有错误处理连接。我的意思是如果你直接在代码中使用它们,你应该总是使用这个习语:
Connection conn = null;
try {
conn = ... ; // get connection
// do stuff
} finally {
if (conn != null) try { conn.close(); } catch (Exception e) { }
}
If you're not released connections back to the pool and are waiting for the garbage collector to clean them up and release them you're going to use way more connections that you actually need.
如果您没有将连接释放回池并等待垃圾收集器清理它们并释放它们,那么您将使用更多您实际需要的连接。
回答by Thorbj?rn Ravn Andersen
First thing to check is whether you have resource leaks. Try surveilling with jconsole or jvisualvm to see how your application behaves and if there is anything that springs in the eye.
首先要检查的是您是否有资源泄漏。尝试使用 jconsole 或 jvisualvm 进行监视,以查看您的应用程序的行为以及是否有任何内容出现在眼睛中。
Then the actual jdbc pool connection is inherently Java EE container specific so you need to give more information.
那么实际的 jdbc 池连接本质上是特定于 Java EE 容器的,因此您需要提供更多信息。
回答by TofuBeer
Are you sure you are closing everything you need to? Take a look here.
你确定你正在关闭你需要的一切吗?看看这里。
Make sure that the close goes in the finally block. You will see this code in the link:
确保 close 在 finally 块中。您将在链接中看到此代码:
finally
{
closeAll(resultSet, statement, connection);
}
I helped someone else find a similar issue (theirs was a memory issue instead... but if the process had gone on longer it would have had this result) where they had not closed the result set or the connection.
我帮助其他人找到了一个类似的问题(他们的问题是内存问题……但如果进程持续更长时间,它就会有这个结果),他们没有关闭结果集或连接。
回答by Tomas
I think you may need to inspect ask yourself some questions, here are some to think about:
我认为您可能需要检查问自己一些问题,以下是一些需要考虑的问题:
A. Are you using youre connections correctly, are you closing them and returning them to the pool after usage?
A. 您是否正确使用您的连接,是否关闭它们并在使用后将它们返回到池中?
B. Are you using long running transactions, e.g. "conversations" with users? Can they be left hanging if the user terminates his usage of the application?
B. 您是否使用长时间运行的事务,例如与用户的“对话”?如果用户终止他对应用程序的使用,它们可以被挂起吗?
C. Have you designed your data access to fit your application? E.g. are you using caching techniques in areas where you expect repeated reads frequently?
C. 您是否设计了适合您的应用程序的数据访问?例如,您是否在期望频繁重复读取的区域使用缓存技术?
D. Are your connection pool big enough? 5 years ago I had a application with 250 simultaneous connections towards a Oracle database, a lot more than what you typically find out of the box. running the application on say 50 didn't work.
D. 你的连接池够大吗?5 年前,我有一个与 Oracle 数据库同时连接 250 个的应用程序,这比通常开箱即用的连接要多得多。在说 50 上运行应用程序不起作用。
To give more detailed answer, you need to provide more info on the app.
要提供更详细的答案,您需要提供有关该应用程序的更多信息。
Good luck!
祝你好运!

