code: public synchronized Connection getConnection() throws SQLException {
          if ((currentTimeMillis() - lastChecked)
          > VALIDITY_CHECK_INTERVAL) {
              logger.trace("Checking connection validity");
              // check the validity of the connection
              boolean valid = false;
              int t = 0;
  
              while (!valid && t < REATTEMPTS) {
                  try {
                      if (connection == null || connection.isClosed()
                              || !connection.isValid(VALIDITY_TIMEOUT)) {
                          logger.info(
                                  "Connection to {} is closed or invalid; attempting to re-establish",
                                  url);
                          // remove any cached prepared statements for this connection
                          threadedPsMap.remove();
  
                          // attempt re-establishment
                          this.connection = DriverManager
                                  .getConnection(url, user, pass);
                          logger.info("Re-established connection to {}", url);
                          // do not set valid to true - check this new connection
                      } else {
                          valid = true;
                      }
                  } catch (SQLException e) {
                      logger.info("Failed to re-establish connection to " +
                              "{}, try {}/{}; Reason: {}",
                              new Object[] { url, t, REATTEMPTS, e.getMessage() });
                      // set connection to null to prevent the same error from continuously
                      // occurring when checking isClosed() or isValid()
                      this.connection = null;
                  }
                  t++;
              }
              if (!valid) {
                  logger.error(
                          "Failed to re-establish a connection to {} after {} attempts",
                          url, REATTEMPTS);
                  throw new SQLException(
                          "Unable to re-establish a valid connection",
                          new DBConnectionFailure(url,
                                  "Unable to re-establish a valid connection"));
              }
              this.lastChecked = currentTimeMillis();
          }
          return connection;
      }
docs: Returns the {@link Connection connection}.
  
  @return The database connection
  @throws SQLException Thrown if the connection is no longer valid and
  cannot be re-established