我已经使用简单的 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:在 org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource)的 org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1171)等待空闲对象超时.java:106) 在 org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
上下文 ctx=new InitialContext(); 120: 数据源 ds=(数据源)ctx.lookup("java:comp/env/jdbc/red"); 121: con=ds.getConnection();