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.