1

Having an odd issue attempting to query a MySQL database; I'm getting a "too many connections" error on my first call to 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.*;

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.

Community
  • 1
  • 1
chris
  • 2,404
  • 3
  • 27
  • 33
  • is this how you calling findEvents? only once? – Sleiman Jneidi May 28 '14 at 16:15
  • Your code looks correct to me (or at least like it should work). The error is in any case coming from the server. So you probably want to look at how that is configured and who else is connecting to the db. You could try bumping the number of connections in my.cnf. – Jilles van Gurp May 28 '14 at 16:21
  • @SleimanJneidi Yes only calling once. Fails on *first* attempt. – chris May 28 '14 at 16:25
  • @jilles-van-gurp I was thinking that, but its a remote server (not in my control), and I can successfully connect from a SQL Client (Navicat, OSX). – chris May 28 '14 at 16:28
  • @JillesvanGurp You're correct, actually. My SQL Native client was connecting to a different db (I'd put the config in different but mistakenly gave it same name). So I get the same error when connecting with my SQL Client, confirming its the server. If you post as Answer I'll accept; – chris May 28 '14 at 18:21
  • Show your code where you "close all connections" please – zmf May 28 '14 at 18:24
  • @zmf its not related. Even if I closed it incorrectly - or not at all - why would it fail on the first attempt? See my comment above - its the server being maxed out by other clients. – chris May 28 '14 at 18:31
  • 1
    check the number of connected clients SHOW STATUS WHERE `variable_name` = 'Threads_connected'; – sherif May 28 '14 at 18:36
  • 1
    @chris if previous versions of this program ran and had old connections hanging around it could be contributing to the fact that you can't get a connection now. log into the db, show processlist; kill connections. try again – zmf May 28 '14 at 18:56

2 Answers2

1

As pointed out by SleimanJneidi and zmf in the comments, the issue was not with my code at all, but the Server itself; 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. This can be found in their documention:

The number of connections permitted is controlled by the max_connections system variable. The default value is 151 to improve performance when MySQL is used with the Apache Web server. (Previously, the default was 100.) If you need to support more connections, you should set a larger value for this variable.

mysqld actually permits max_connections+1 clients to connect. The extra connection is reserved for use by accounts that have the SUPER privilege. 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. See Section 13.7.5.30, “SHOW PROCESSLIST Syntax”.

chris
  • 2,404
  • 3
  • 27
  • 33
-1

u have to put

                if(conn != null) {
                    try {
                    conn.close();   
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 07 '21 at 08:38