title: JDBC MySQL Connection Issue - Attempted reconnect 3 times. Giving up
body: <p>I have a rest service application running the Java Spring framework. The application depends on a connection to an external MySQL DB, which is connected via JDBC.</p>
  
  <p>My issue is maintaining a solid connection between the rest service and the MySQL db. I have what I consider a rudimentary connection failsafe in place that looks something like:</p>
  
  <pre><code>public Connection getConnection() throws SQLException {
      if(connection == null){
           this.buildConnection();
      }
      else if(!connection.isValid(10)){ //Rebuild connection if it is no longer valid
          connection.close();
          this.buildConnection();
      }
      return connection;
  }
  </code></pre>
  
  <p>Using this method should ensure that the connection is valid before any query is executed. My problem is that I periodically get an exception thrown when calling this method: </p>
  
  <blockquote>
    <p>Could not create connection to database server. Attempted reconnect 3 times. Giving up. SQLState: 08001. ErrorCode: 0.</p>
  </blockquote>
  
  <p>The things that have me super perplexed about this are:</p>
  
  <ol>
  <li>This error only happens periodically. Many times the connection works just find. </li>
  <li>I test this same application on my developer machine and this error never occurs.</li>
  </ol>
  
  <p>I custom configured the MySQL DB on my own server, so I control all its config options. From this I know that this issue isn't related to the maximum number of connections allowed, or a connection timeout.</p>
  
  <p><strong>Edit - Update 1:</strong> </p>
  
  <ul>
  <li>This service is hosted as Cloud Service on Microsoft Azure platform. </li>
  <li>I accidentally set it up as an instance in Northern Europe, while the DB is located in North America - probably not related, but trying to paint the whole picture. </li>
  <li>Tried the advice at <a href="https://stackoverflow.com/questions/15813522/jdbcexceptionreporter-sql-error-0-sqlstate-08001">this link</a> with no success. Not using thread pools, and all ResultSets and Statements/PreparedStatements are closed after. </li>
  </ul>
  
  <p><strong>Edit - Update 2</strong></p>
  
  <p>After some refactoring, I was able to successfully implement a HikariCP Connection Pool as outlined by @M.Deinum below. Unfortunately, the same problem persists. Everything works great on my local machine, and all Unit Tests pass, but as soon as I push it to Azure and wait more than a few minutes between requests, I get this error, when trying to grab a connection from the pool:</p>
  
  <blockquote>
    <p>springHikariCP - Connection is not available, request timed out after 38268ms.  SQLState: 08S01. ErrorCode: 0.</p>
  </blockquote>
  
  <p>My HikariCP configuration is as follows:</p>
  
  <pre><code>//Set up connection pool
  HikariConfig config = new HikariConfig();
  config.setDriverClassName("com.mysql.jdbc.Driver"); 
  config.setJdbcUrl("jdbc:mysql://dblocation");
  
  //Connection pool properties
  Properties prop = new Properties();
  prop.setProperty("user", "Username");
  prop.setProperty("password", "Password");
  prop.setProperty("verifyServerCertificate", "false");
  prop.setProperty("useSSL","true");
  prop.setProperty("requireSSL","true");
  config.setDataSourceProperties(properties);
  
  config.setMaximumPoolSize(20);
  config.setConnectionTestQuery("SELECT 1");
  config.setPoolName("springHikariCP");
  config.setLeakDetectionThreshold(5000); 
  
  config.addDataSourceProperty("dataSource.cachePrepStmts", "true");
  config.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
  config.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
  config.addDataSourceProperty("dataSource.useServerPrepStmts", "true");
  
  dataSource = new HikariDataSource(config);
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>