Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误:数据源拒绝建立连接,来自服务器的消息:";“连接太多”;_Java_Mysql_Jdbc_Mysql Error 1064 - Fatal编程技术网

Java 错误:数据源拒绝建立连接,来自服务器的消息:";“连接太多”;

Java 错误:数据源拒绝建立连接,来自服务器的消息:";“连接太多”;,java,mysql,jdbc,mysql-error-1064,Java,Mysql,Jdbc,Mysql Error 1064,我有一个文本文件,它的大小不是很重(600KB),我的代码应该读取该文件并将整个包含内容发送到我的数据库: private static void readBigFileAndSendToDB() throws Exception { Scanner s = new Scanner(new FileReader("tf1.txt")); while (s.hasNextLine()) { String eachLine = s.nextLine();

我有一个文本文件,它的大小不是很重(600KB),我的代码应该读取该文件并将整个包含内容发送到我的数据库:

    private static void readBigFileAndSendToDB() throws Exception {
    Scanner s = new Scanner(new FileReader("tf1.txt"));
    while (s.hasNextLine()) {
        String eachLine = s.nextLine();   // each Line
        sendBigFileToDB(eachLine);
    }                                    // (end of file).
    System.out.println("Sent big file to DB");
    s.close();
}


    private static void sendBigFileToDB(String line) {
    Connection con = null;
    PreparedStatement ps = null;
    String query = "Insert into BigFile(text) values ('" + line + "')";

    try {
        if (line != null) {
            con = DriverManager.getConnection(...);
            ps = con.prepareStatement(query);
            ps.execute();
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
但当我运行程序时,出现了以下异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"

请在完成流程后关闭语句并关闭连接:

private static void readBigFileAndSendToDB() throws Exception {
    Scanner s = new Scanner(new FileReader("tf1.txt"));
    while (s.hasNextLine()) {
        String eachLine = s.nextLine();   // each Line
        sendBigFileToDB(eachLine);
    }                                    // (end of file).
    System.out.println("Sent big file to DB");
    s.close();
}


    private static void sendBigFileToDB(String line) {

    PreparedStatement ps = null;
    String query = "Insert into BigFile(text) values ('" + line + "')";

    try {
        if (line != null) {
            con = DriverManager.getConnection(...);
            ps = con.prepareStatement(query);
            ps.execute();               
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    finally {
        if (ps != null) try { ps .close(); } catch (SQLException logOrIgnore) {}
        if (con != null) try { con .close(); } catch (SQLException logOrIgnore) {}
        }
     }

当您获得连接时,还需要关闭它。不关闭连接会导致资源泄漏,这可能最终导致服务器拒绝连接,因为已达到最大连接数

我建议您更改代码,以便在资源的使用范围结束时自动关闭资源:

if (line == null) return;
String query = "Insert into BigFile(text) values (?)";

try (
    Connection con = con = DriverManager.getConnection(...);
    PreparedStatement ps = con.prepareStatement(query);
){
    ps.setString(1, line);
    ps.executeUpdate();
} catch (SQLException sqle) {
    sqle.printStackTrace();
}
请注意,我还将
连接到查询中替换为正确使用prepared语句参数

通过将连接创建和语句准备移到
readBigFileAndSendToDB
中,您的代码可以得到进一步的改进,这样只需执行一次,并且不会在每次迭代时产生开销:

String query = "Insert into BigFile(text) values (?)";

try (
    Scanner s = new Scanner(new FileReader("tf1.txt"));
    Connection con = con = DriverManager.getConnection(...);
    PreparedStatement ps = con.prepareStatement(query);
){
    while (s.hasNextLine()) {
        String line = s.nextLine();
        if (line == null) continue;
        ps.setString(1, line);
        ps.addBatch();
    }
    ps.executeBatch();
} catch (SQLException sqle) {
    sqle.printStackTrace();
}

尝试关闭连接,一旦完成,我建议尝试使用资源,使用
executeUpdate()
替换
executeBatch()
关闭资源要简单得多。我是否应该将
resultset对象初始化语句
移到
Try()
?(这不是问题)@user3808021是的,你应该这样做,这将确保它在try块结束后关闭。