def _MakeConnection(self, database=""):
    """Repeat connection attempts to server until we get a valid connection."""
    first_attempt_time = time.time()
    wait_time = config.CONFIG["Mysql.max_connect_wait"]
    while wait_time == 0 or time.time() - first_attempt_time < wait_time:
      try:
        connection_args = dict(
            user=config.CONFIG["Mysql.database_username"],
            db=database,
            charset="utf8",
            passwd=config.CONFIG["Mysql.database_password"],
            autocommit=True,
            cursorclass=cursors.DictCursor,
            host=config.CONFIG["Mysql.host"],
            port=config.CONFIG["Mysql.port"])

        key_path = config.CONFIG["Mysql.client_key_path"]
        if key_path:
          cert_path = config.CONFIG["Mysql.client_cert_path"]
          ca_cert_path = config.CONFIG["Mysql.ca_cert_path"]
          logging.debug("Client key file configured, trying to use SSL.")

          connection_args["ssl"] = {
              "key": key_path,
              "cert": cert_path,
              "ca": ca_cert_path,
          }

        return MySQLdb.connect(**connection_args)

      except MySQLdb.OperationalError as e:
        # This is a fatal error, we just raise the top level exception here.
        if "Access denied" in str(e):
          raise Error(str(e))

        if "Can't connect" in str(e):
          logging.warning("Datastore connection retrying after failed with %s.",
                          str(e))
          time.sleep(.5)
          continue
        raise

    raise IOError("Unable to connect to Mysql database.")
Repeat connection attempts to server until we get a valid connection.