Topic: 各位高手快救救我 (运行一段时间后就会说数据库连接过多,运行不了)

  Print this page

1.各位高手快救救我 (运行一段时间后就会说数据库连接过多,运行不了) Copy to clipboard
Posted by: chengshwu
Posted on: 2003-03-20 12:55

我是用mysql数据库,进行操作很多,原码如下:
class mysqldatabean {
String mysqlDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String mysqlConnStr = "jdbc:odbc:mysqldata";
Connection mysqlconn = null;
ResultSet mysqlrs = null;
Statement mysqlstmt=null;
public mysqldatabean() {
try {
Class.forName(mysqlDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("mysqlexecuteQuery: " + e.getMessage());
}
}

public ResultSet mysqlexecuteQuery(String mysqlsql) {
try {
mysqlconn = DriverManager.getConnection(mysqlConnStr);
mysqlstmt = mysqlconn.createStatement();
mysqlrs = mysqlstmt.executeQuery(mysqlsql);
}
catch(SQLException ex) {
System.err.println("mysqlexecuteQuery: " + ex.getMessage());
}
return mysqlrs;
}

public void destroyconnection(){
boolean connboolean=false;
try{
mysqlconn.close();

connboolean=mysqlconn.isClosed();
}
catch(SQLException ex){}
}
}

上面的是进行连接mysql的类,然后在主程序中引用executeQuery方法,(引用了很多次),每次我都用这个类中的destroyconnection方法断开了连接,可运行一段时间后就会说数据库连接过多,运行不了了!
请高手指教

2.Re:各位高手快救救我 [Re: chengshwu] Copy to clipboard
Posted by: floater
Posted on: 2003-03-20 23:53

do a search.

3.Re:各位高手快救救我 [Re: chengshwu] Copy to clipboard
Posted by: snowbug
Posted on: 2003-03-21 01:16

Do you use the same instance of the mysqlconnbean to do all the queries? or each time you will create a new instance of the mysqlconnbean?

If you use the same instance, then each time when you do a new query, a new connection is created, and you loose the reference to the previous connections object in this mysqlconnbean, which means that those connections are never closed.

另外,下次提问可否使用更有意义的标题,比如:mysql 连接数目过多错误,...

Another thing is, you should use the mysql JDBC library to connect to the mysql db, which will perform better than going through JDBC ODBC bridge.

4.Re:各位高手快救救我 [Re: chengshwu] Copy to clipboard
Posted by: flytiger
Posted on: 2003-03-21 04:40

//How about this?

public void destroyconnection(){
boolean connboolean=false;
try{
mysqlstmt.close();
mysqlrs.close();
mysqlconn.close();

connboolean=mysqlconn.isClosed();
}
catch(SQLException ex){}
}
}

5.Re:各位高手快救救我 [Re: chengshwu] Copy to clipboard
Posted by: floater
Posted on: 2003-03-25 00:28

do a search to find the right piece of code.

You need to close several things related to resources. And you may say "I have the code for closing there", but that piece could not be run since exceptions are thrown before that line. Say, you have
...
connection.close();
resultset.close();

The second close() may not run at all(if connection=null).

6.Re:各位高手快救救我 [Re: chengshwu] Copy to clipboard
Posted by: why
Posted on: 2003-03-25 01:12

seems that chengshwu has never been back and will not be back...
BTW, can a moderator edit the Subject? a meaningful one is wanted. Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923