2012-07-19 54 views
0

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

http://stackoverflow.com/questions/1392304/com-mysql-jdbc-exceptions-jdbc4-mysqlnontransientconnectionexception-in-mysql – kosa 2012-07-19 15:23:50

+0

如果您在工作台检查,怎么样许多连接实际上是开放的?小于允许的最大值?你认为所有关闭的连接是否也关闭? – fvu 2012-07-19 15:25:51

+3

在断开连接之前您打几次连接?还有一件事..每件事情都是静态的..如果你连接()connect()disconnect()你将如何断开第一个连接? – 2012-07-19 15:41:01

回答

0

正常情况下,用户的可用连接数量有限。另外,建立连接是昂贵的操作。因此,如果你遇到性能问题,你应该开始使用连接池,discussion about connection pools

关于你的代码,确保你总是在finally块中释放连接。我们也不知道你运行的是什么类型的查询。打印您的查询并尝试在mysql中手动运行它们,并查看问题是否是您的SQL,而不是Java。此外,你永远不会关闭FileInputStream,这意味着你可以用完文件处理程序。

如果您在循环中运行相同的语句,请考虑使用PreparedStatements以及批量更新和事务。如果您使用事务,请确保您没有太多数据处于未提交状态。

如果你需要分析你的查询性能,您可以使用JAMon (Java Application Monitor)

建议增加连接数类似于一个人腹泻建议吃更多的食物。

0

如果您使用数据库的开发者版本,它可能会对可以建立的连接数量进行限制。所以你需要更好地管理它们,这意味着当你完成它们时你应该确保它们被关闭。 (编辑:刚刚意识到你使用MySQL所以这不应该是一个问题)

另外,你提到你的调试器正在经历的意见 - 这通常是在您的代码之间不同步的情况下你的构建。清理你的构建(在eclipse中它会是项目>清理...>清理所有项目),该问题应该消失。

+0

我试图清理所有项目,但同步问题仍然存在:( – Stiva 2012-07-19 16:18:30

1

断开方法可能需要以下增强。在这个中,我们分别关闭ResultSet,Statement & Connection。这可以节省单个异常不会导致不关闭连接对象的情况。

public static void disconnect() throws RemoteException { 
     try{ 
      if(rs != null) rs.close(); 
     } 
     catch(SQLException sqlEx){ 
      System.out.println("Error: disconnect"); 
     } 


     try{ 
      if(stat != null) stat.close(); 
     } 
     catch(SQLException sqlEx){ 
      System.out.println("Error: disconnect"); 
     } 

     try{ 
      if(cn != null) cn.close(); 
     } 
     catch(SQLException sqlEx){ 
      System.out.println("Error: disconnect"); 
     } 
    } 
0

试试这个

if(cn == null){ 
    String driver = "com.mysql.jdbc.Driver"; 
    Class.forName(driver); 
    dbHost = "jdbc:mysql://"+dbHost; 
    cn = DriverManager.getConnection(dbHost,dbUser,dbPassword); 
}