我认为我的应用程序被诅咒了,调试去了它想要的地方,我不知道为什么。逐行调试似乎也分析了注释行。我认为问题出在我的 Connection 方法上,我发现性能显着下降,并且在第三个(或第四个 nvm)连接处出现此错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
我确定每次访问数据库时都会关闭连接(在实现中的 try catch 中使用 finally 语句)。
这是我的连接类:
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");
}
}
}