java Tomcat连接池配置:数据源类型和“连接太多”错误
我正在通过JNDI资源使用tomcat连接池
在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" />
在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>
从我需要db连接的java类中,我进行以下查找:
Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
我的第一个疑问是数据源类型。使用javax.sql.DataSource
或org.apache.tomcat.jdbc.pool.DataSource
是一样的吗
此外,有时我会出现“连接太多”的错误。 我读过很多关于这个问题的问答,但我没有成功地理解问题可能在哪里
我遵循了tomcat文档,并正确关闭了结果集、语句和连接
编辑
我的tomcat版本是7.0.26。所以应该有一个bug(参见informatik01用户建议的链接)
# 1 楼答案
您可能需要增加mysql上的最大连接数,默认最大连接数为151