日期:2014-05-17  浏览次数:20756 次

jsp连接SQL2008超时,求各位大神指点!
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:114)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at uploadtest.ConnectionDB.getConnection(ConnectionDB.java:18)
at ado.productado.page_count(productado.java:152)
at webtier.ProductController.readproduct(ProductController.java:215)
at webtier.ProductController.doGet(ProductController.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
我的每个连接资源都有CLOSE(),报错指向java类,我用的是连接池,之前是在SQL2005开发,连接数据库没有问题!后面将数据库转换成2008就出现这个毛病了!
异常指向java类的方法:
  public int page_count() throws NamingException, SQLException{
int count = 0;
int pages = 0;
try {
conn=ConnectionDB.getConnection();
String sql="select * from product";
Statement smt=null;
try {
smt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=smt.executeQuery(sql);
rs.last();
count=rs.getRow();
pages=count/12;
if(count%12>0){
pages=pages+1;
return pages;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pages;
}


------解决方案--------------------
我看你的代码里也没有smt.close(),conn.close()类似的代码啊。。
------解决方案--------------------
真正原因是数据库连接没有及时关闭,导致数据连接池满了,无法再取得连接。

解决方法:每次调用数据库链接以后要关闭,如手工调用Hibernate的session.close(),直接使用JDBC则同样需要关闭,或是使用spring的事务处理!


------解决方案--------------------
就是说,你上面的代码,没有关闭连接,如果要打开数据库,必须关闭连接,释放资源conn.close();
------解决方案--------------------
public static Connection getConnection() throws Exception
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://190.160.10.64:1433;DatabaseName=website";
String user="sa";
String password = "123456";
return DriverManager.getConnection(url, user, password);
}