Data source rejected establishment of connection, message from server: "Too many connections"解决办法

异常名称

//数据源拒绝从服务器建立连接、消息:“连接太多”
com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"

原因一

  • MYSQL安装目录打开MY.INI。
  • 找到max_connections(在大约第93行)默认是100 一般设置到500~1000比较合适。
  • max_connections=1000
  • 重启mysql,这样1040错误就解决啦。

原因二

  还有一个可能就是代码里打开了太多的连接,但是忘记了在finally块里面关闭,从而导致在处理大数据的时候,抛出异常。下面这样的代码就不会有异常了。

try{            
        conn=Good.getConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String sql1="insert into cat_garbage values('"+rs.getInt("id")+"','"+rs.getInt("cid")+"','"+rs.getString("name")+"','"+rs.getString("keyword")+"')";
        stmt.executeUpdate(sql1);
        }
        catch(SQLException|ClassNotFoundException|IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(stmt!= null) 
                stmt.close();         
            if(conn!= null) 
                conn.close(); 
        }
   }

本文章参考:吴孟达

原文地址:https://www.cnblogs.com/wkrbky/p/6232266.html