java Tomcat 连接池配置:数据源类型和“连接过多”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14760177/
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
Tomcat Connection Pool configuration: DataSource type and "Too many connection" error
提问by Sefran2
I'm using the tomcat connection pool via JNDI resources.
我正在通过 JNDI 资源使用 tomcat 连接池。
In the context.xml
:
在context.xml
:
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
maxActive="1000" maxIdle="100" maxWait="10000"
url="jdbc:mysql://localhost:3306/mydatabase"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" />
In web.xml
:
在web.xml
:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
From the java classes in which I need db connections, I do this lookup:
从我需要数据库连接的 java 类中,我执行以下查找:
Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
My first doubt is the DataSource type. Is it the same using javax.sql.DataSource
or org.apache.tomcat.jdbc.pool.DataSource
?
我的第一个疑问是 DataSource 类型。使用javax.sql.DataSource
或是一样的org.apache.tomcat.jdbc.pool.DataSource
吗?
Moreover, sometimes I obtain a "Too many connections" error. I've read many stackoverflow question/answers about this, but I don't succeed in understanding where the problem could be.
此外,有时我会收到“连接过多”错误。我已经阅读了很多关于此的 stackoverflow 问题/答案,但我没有成功理解问题可能出在哪里。
I have followed the tomcat docs, and I close properly result sets, statements and connection.
我遵循了 tomcat 文档,并正确关闭了结果集、语句和连接。
EDIT
编辑
My tomcat version is 7.0.26. So there should be a bug (see link suggested by informatik01 user)
我的 tomcat 版本是 7.0.26。所以应该有一个错误(见informatik01用户建议的链接)
回答by user1681732
If you put the JDBC resource in the $CATALINA_HOME/conf/context.xml it loads the resource for every single webapp you have deployed. (Which can mean a huge number of connections) If you move that resource to META-INF/context.xml of your webapp it will only load when that specific webapp is deployed. http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
如果您将 JDBC 资源放在 $CATALINA_HOME/conf/context.xml 中,它会为您部署的每个 Web 应用程序加载资源。(这可能意味着大量连接)如果您将该资源移动到您的 web 应用程序的 META-INF/context.xml,它只会在部署该特定 web 应用程序时加载。http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
It could also be that you have way too many maxActive and maxIdle.
也可能是你有太多的 maxActive 和 maxIdle。
回答by Ravindra Gullapalli
javax.sql.DataSource
is an interface and org.apache.tomcat.jdbc.pool.DataSource
is a class. I am not sure if tomcat permits us to directly instantiate org.apache.tomcat.jdbc.pool.DataSource
. If yes, you can use any of these.
javax.sql.DataSource
是一个接口,org.apache.tomcat.jdbc.pool.DataSource
是一个类。我不确定 tomcat 是否允许我们直接实例化org.apache.tomcat.jdbc.pool.DataSource
. 如果是,您可以使用其中任何一个。
The connection related error could be due to
连接相关的错误可能是由于
maxActive="1000" maxIdle="100" maxWait="10000"
in your tomcat configuration file.
maxActive="1000" maxIdle="100" maxWait="10000"
在您的 tomcat 配置文件中。
Set it to maxActive="10" maxIdle="10" maxWait="10"
- 10 number of active connections, 10 number of idle connections with a maximum 10 seconds wait time.
将其设置为maxActive="10" maxIdle="10" maxWait="10"
- 10 个活动连接数,10 个空闲连接数,最长等待时间为 10 秒。
回答by Stéphane
You may need to increase the max connection on mysql , the default max is 151.
您可能需要增加 mysql 上的最大连接数,默认最大值为 151。
回答by Nestor Hernandez Loli
Make sure you don't have a resource leak: Example java.sql.Connection not getting closed
确保您没有资源泄漏:示例 java.sql.Connection not getting closed