JAVA:MySql:连接太多了

时间:2012-07-19 15:20:51

标签: java mysql jdbc connection

我认为我的应用程序受到了诅咒,调试进入了它想要的地方,我不知道为什么。 逐行调试似乎也分析了注释行。 我认为问题出在我的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");
            }   
      }
}

5 个答案:

答案 0 :(得分:1)

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

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");
        }   
  }

答案 1 :(得分:0)

我认为你可以尝试一些选项:

答案 2 :(得分:0)

如果您使用的是数据库的开发人员版本,则可能会限制可以建立的连接数。因此,您需要更好地管理它们,这意味着您应该确保在完成它们后关闭它们。 (编辑:刚刚意识到你正在使用mysql所以这应该不是问题)

此外,您提到您的调试器正在进行注释 - 通常情况下,您的代码与您的构建不同步。清理你的构建(在eclipse中它将是Project> clean ...>清理所有项目)并且该问题应该消失。

答案 3 :(得分:0)

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

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

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

如果您想分析查询的效果,可以使用JAMon (Java Application Monitor)

增加连接数量的建议类似于建议腹泻患者多吃一些食物。

答案 4 :(得分:0)

试试这个

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