class MySqlConnection(object): def __init__(self, host): self.host = host def connect(self): """Connect to MySQL database.""" print("Connecting to MySQL, mysql --host %s -u %s -p%s" % (self.host, MYSQL_USERNAME, MYSQL_PASSWORD)) sql_engine = LocalSqlClient.init_engine(MYSQL_USERNAME, MYSQL_PASSWORD, self.host) self.client = LocalSqlClient(sql_engine, use_flush=False) def is_connected(self): cmd = "SELECT 1;" try: with self.client: self.client.execute(cmd) return True except Exception as e: print("Failed to execute command: %s, error: %s" % (cmd, str(e))) return False def execute(self, cmd): try: with self.client: self.client.execute(cmd) return True except Exception as e: print("Failed to execute command: %s, error: %s" % (cmd, str(e))) return False
class MySqlConnection(object): def __init__(self, host): self.host = host def connect(self): """Connect to MySQL database.""" print("Connecting to MySQL, mysql --host %s -u %s -p%s" % (self.host, MYSQL_USERNAME, MYSQL_PASSWORD)) sql_engine = LocalSqlClient.init_engine(MYSQL_USERNAME, MYSQL_PASSWORD, self.host) self.client = LocalSqlClient(sql_engine, use_flush=False) def is_connected(self): try: with self.client: self.client.execute(text("""SELECT "Hello.";""")) return True except (sqlalchemy_exc.OperationalError, sqlalchemy_exc.DisconnectionError, sqlalchemy_exc.TimeoutError): return False except Exception as ex: print("EX WAS:") print(type(ex)) print(ex) raise ex