简体   繁体   English

使用 JDBC 获取到 MySQL 的连接时“连接过多”

[英]"Too many Connections" when obtaining connection to MySQL using JDBC

Having an odd issue attempting to query a MySQL database;尝试查询 MySQL 数据库时遇到奇怪的问题; I'm getting a "too many connections" error on my first call to getConnection .我在第一次调用 getConnection 时收到“太多连接”错误。 Surely I must be doing something idiotic?我一定是在做一些愚蠢的事情吗? For reference, I know the basics of closing connection , and not only am I only making one call, its failing on the very first attempt.作为参考,我知道关闭连接的基础知识,而且我不仅只打了一个电话,而且第一次尝试就失败了。 So I must be doing something extremely stupid?所以我一定是在做一些非常愚蠢的事情?

Two classes, one to call for a query, and the other to execute it: import java.sql.*;两个类,一个调用查询,另一个执行查询:import java.sql.*;

public class Main {

    public static void main(String[] args) {

        CollectorDb.findEvents();
    }
}

public class CollectorDb {
    private static String username = "foo";
    private static String password = "bar";
    private static String connectionString = "jdbc:mysql://awsdb/dbName" +
            "?characterEncoding=UTF-8";

    public static void findEvents() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        try {
            // Next line throws Exception
            conn = DriverManager.getConnection(connectionString, username, password);
            // ** NEVER GET HERE **
            // single query, then close all connections in a finally block
            ...
    }

}

First lines of the stacktrace:堆栈跟踪的第一行:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
...
Exception in thread "main" java.lang.RuntimeException: Too many connections
    at com.lifesize.CollectorDb.findEvents(CollectorDb.java:64)
    at com.lifesize.Main.main(Main.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
...

Have truncated the details that I think aren't relevant - happy to provide more code or full stacktrace, or any other information of course.截断了我认为不相关的细节 - 很乐意提供更多代码或完整的堆栈跟踪,或者当然是任何其他信息。

As pointed out by SleimanJneidi and zmf in the comments, the issue was not with my code at all, but the Server itself; 正如SleimanJneidi和zmf在评论中指出的那样,问题根本不在我的代码中,而是服务器本身。 the biggest clue being failure to connect on first attempt . 最大的线索是初次尝试失败

In my case, I don't have direct access to the server, but the following will display current connected clients: 就我而言,我没有直接访问服务器的权限,但是以下内容将显示当前连接的客户端:

SHOW STATUS WHERE variable_name = 'Threads_connected';

To answer my follow up of "How can I connect to check this if max connections are exceeded" - MySQL reserves a connection for the admin , specifically for this reason. 为了回答我的后续文章“如果超过最大连接数,我该如何连接以进行检查” -MySQL为此保留了一个用于admin的连接 This can be found in their documention : 这可以在他们的文档中找到:

The number of connections permitted is controlled by the max_connections system variable. 允许的连接数由max_connections系统变量控制。 The default value is 151 to improve performance when MySQL is used with the Apache Web server. 当MySQL与Apache Web服务器一起使用时,默认值是151,以提高性能。 (Previously, the default was 100.) If you need to support more connections, you should set a larger value for this variable. (以前,默认值为100。)如果需要支持更多连接,则应为此变量设置一个较大的值。

mysqld actually permits max_connections+1 clients to connect. mysqld实际上允许max_connections + 1客户端连接。 The extra connection is reserved for use by accounts that have the SUPER privilege. 保留额外的连接供具有SUPER特权的帐户使用。 By granting the SUPER privilege to administrators and not to normal users (who should not need it), an administrator can connect to the server and use SHOW PROCESSLIST to diagnose problems even if the maximum number of unprivileged clients are connected. 通过向管理员而非超级用户(不需要该超级用户)授予SUPER特权,即使连接了最大数量的非特权客户端,管理员也可以连接到服务器并使用SHOW PROCESSLIST诊断问题。 See Section 13.7.5.30, “SHOW PROCESSLIST Syntax”. 请参见第13.7.5.30节“ SHOW PROCESSLIST语法”。

u have to put你必须把

                if(conn != null) {
                    try {
                    conn.close();   
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM