title: Java/JDBC/MySQL: How do I troubleshoot why DriverManager.getConnection() is returning NULL?
body: <p>I am attempting to troubleshoot an issue with a Java application that is utilizing JDBC to connect to a MySQL database. The surface issue is that when connecting to a valid database, DriverManager.getConnection will sometimes return NULL, while just a few minutes later it will return a valid connection to the exact same database. </p>
  
  <p>I am in the position of trying to troubleshoot this issue, but my understanding of where Java, JDBC, and MySQL meet is fairly limited. I've been doing lots of research on this, but have hit a wall and don't know where to go from here.</p>
  
  <p>Here's what I've done so far:</p>
  
  <ul>
  <li>On the Java end, I have traced the code all the way to DriverManager.getConnection(). I have determined that the NULL connection is coming from there, but I have no clue what goes on under the hood of getConnection. I've been struggling to find a thorough explanation of this online.</li>
  <li>On the MySQL end, I have verified that there are plenty of connections available to me (around 1000 free connections at times), so I know I'm not exceeding max connections there. Viewing logs, I was able to determine that there are a slightly higher number of connections being aborted during the time-frames where I'm having the most issues, but I don't know how to determine why these connections were aborted (did MySQL abort, JDBC, the Java application?) I'm not sure if there is anything else I need to be looking for on the MySQL end.</li>
  <li>In the middle, with JDBC, I'm pretty lost. I have been reading up on MySQL Connector/J at <a href="http://dev.mysql.com/doc/refman/5.1/en/connector-j.html" rel="noreferrer">http://dev.mysql.com/doc/refman/5.1/en/connector-j.html</a>, but am not certain if that info pertains to the JDBC driver being utilized by Java.</li>
  </ul>
  
  <p>Any direction as to where I can go from here would be much appreciated.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT - 2/15, 10:35am CST</strong>
  I apologize for not being more specific. This application is a production application that normally works just fine. It's successfully processing tens of thousands of connections a day without any issues, it's just that this issue will crop up at random times during the day, and will persist for anywhere from 30 seconds to 5 minutes when it does happen.</p>
  
  <p>Here's the code that I've traced all the way down to DriverManager.getConnection:</p>
  
  <pre><code>var dbConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver','jdbc:mysql://'+ serverName +':' + port + '/' + database, userName, password);
  
  public static DatabaseConnection createDatabaseConnection(String driver, String address, String username, String password) throws SQLException {
          try {
              Class.forName(driver);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          Properties info = new Properties();
          info.setProperty("user", username);
          info.setProperty("password", password);
  
          // this property should only be set if it's for embedded database
          info.setProperty("shutdown", "true");
  
          return new DatabaseConnection(address, info);
      }
  
  public DatabaseConnection(String address, Properties info) throws SQLException {
          logger.debug("creating new database connection: address=" + address + ", " + info);
          this.address = address;
          connection = DriverManager.getConnection(address, info);
      }
  </code></pre>
  
  <p>I don't believe there is actually any issue with the code, but rather an issue somewhere in between getConnection() and MySQL.</p>