有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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.DataSourceorg.apache.tomcat.jdbc.pool.DataSource是一样的吗

此外,有时我会出现“连接太多”的错误。 我读过很多关于这个问题的问答,但我没有成功地理解问题可能在哪里

我遵循了tomcat文档,并正确关闭了结果集、语句和连接

编辑

我的tomcat版本是7.0.26。所以应该有一个bug(参见informatik01用户建议的链接)


共 (1) 个答案

  1. # 1 楼答案

    您可能需要增加mysql上的最大连接数,默认最大连接数为151