我认为我的应用程序被诅咒,调试去了它想要的地方,我不知道为什么。 逐行调试似乎也分析了注释的行。 我认为,这些问题是在我的连接方法,我看到一个显著性能下降,并且在第三(或第四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