我認爲我的應用程序被詛咒,調試去了它想要的地方,我不知道爲什麼。 逐行調試似乎也分析了註釋的行。 我認爲,這些問題是在我的連接方法,我看到一個顯著性能下降,並且在第三(或第四NVM)方面,我得到這個錯誤:JAVA:MySql:太多的連接
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
我敢肯定,我關閉連接每次我訪問數據庫時(最終的聲明都不在實現中的try catch中)。
這裏是我的連接類:
package Connection;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.rmi.RemoteException; import java.sql.*; import java.util.Properties;
public class ConnectionDB {
public static ResultSet rs = null;
public static Statement stat = null;
public static Connection cn = null;
public static void connect() throws RemoteException {
String dbHost="",dbUser="",dbPassword="";
Properties prop=new Properties();
try
{
//carico il file:
prop.load(new FileInputStream("***/config.properties"));
//Leggo le proprietà del file:
dbHost=prop.getProperty("host");
dbUser=prop.getProperty("user");
dbPassword=prop.getProperty("password");
}catch(FileNotFoundException fe){
System.out.println("config file not found");
}catch(IOException ex){
System.out.println("Error reading config file");
}
try
{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
dbHost = "jdbc:mysql://"+dbHost;
cn = DriverManager.getConnection(dbHost,dbUser,dbPassword);
stat = cn.createStatement();
}catch(SQLException e){
System.out.println("Can't connect to the DB");
}
catch(ClassNotFoundException cln){
System.out.println("Error using JDBC driver");
}
}
public static void disconnect() throws RemoteException {
try{
if(rs != null) rs.close();
if(stat != null) stat.close();
if(cn != null) cn.close();
}
catch(SQLException sqlEx){
System.out.println("Error: disconnect");
}
}
}
http://stackoverflow.com/questions/1392304/com-mysql-jdbc-exceptions-jdbc4-mysqlnontransientconnectionexception-in-mysql – kosa 2012-07-19 15:23:50
如果您在工作臺檢查,怎麼樣許多連接實際上是開放的?小於允許的最大值?你認爲所有關閉的連接是否也關閉? – fvu 2012-07-19 15:25:51
在斷開連接之前您打幾次連接?還有一件事..每件事情都是靜態的..如果你連接()connect()disconnect()你將如何斷開第一個連接? – 2012-07-19 15:41:01