我使用简单的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();