如何在上下文文件中设置连接池?

时间:2012-11-22 09:03:06

标签: mysql eclipse jsp servlets

我使用简单的servlet开发网页。在这里,我使用一个上下文文件来声明这样的数据库连接。

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/red" auth="Container" type="javax.sql.DataSource"
      maxActive="50" timeBetweenEvictionRunsMillis="3600000"
      minEvictableIdleTimeMillis="3600000"
      maxIdle="30" maxWait="10000" username="root"
      password="root" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/red"
      defaultAutoCommit="false" >

</Resource>
</Context>

我在web.xml中配置了

  <resource-ref>
    <description>Mysql datasource</description>
    <res-ref-name>jdbc/red</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
 </resource-ref>

在servlet页面中

    Context ctx=new InitialContext();
    DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/red");
    con=ds.getConnection();
    state=con.createStatement();
    rs=state.executeQuery(s);
    rs.close();
    con.close();
    ctx.close();

我的问题是当连接建立超过50次时发生错误,如下所示:

   SEVERE: Servlet.service() for servlet jsp threw exception

java.util.NoSuchElementException:等待空闲对象的超时     at org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1171)     at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)     在org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)

Context ctx = new InitialContext();    120:DataSource ds =(DataSource)ctx.lookup(“java:comp / env / jdbc / red”);    121:con = ds.getConnection();

1 个答案:

答案 0 :(得分:0)

尝试执行 在MySQL中SET GLOBAL max_connections = 500;或重启MySQL。 此链接将帮助您解决问题 here

相关问题