0

I have a routine that is accessed every second, it worked fine for the days and then gave the error:

Exception in store_price
[2023-02-03 05:02:56] - Traceback (most recent call last):
  File “/x/db.py", line 86, in store_price
    with connection.cursor() as cursor:
  File "/root/.pyenv/versions/3.9.4/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 632, in cursor
    raise OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available

Below is my code

def store_price(connection, symbol, last_price, timestamp):
    """    
    :param connection:
    :param symbol:
    :param last_price:
    :param timestamp:
    :return:
    """
    table_name = 'deribit_price_{}'.format(symbol.upper())
    try:
        if connection is None:
            print('Null found..reconnecting')
            connection.reconnect()

        if connection is not None: # this is line # 86
            with connection.cursor() as cursor:
                sql = "INSERT INTO {} (last_price,timestamp) VALUES (%s,%s)".format(table_name)
                cursor.execute(sql, (last_price, timestamp,))
                connection.commit()
    except Exception as ex:
        print('Exception in store_perpetual_data')
        crash_date = time.strftime("%Y-%m-%d %H:%m:%S")
        crash_string = "".join(traceback.format_exception(etype=type(ex), value=ex, tb=ex.__traceback__))
        exception_string = '[' + crash_date + '] - ' + crash_string + '\n'
        print(exception_string)
Volatil3
  • 14,253
  • 38
  • 134
  • 263
  • Perhaps you should checked whether the connection is closed, and reconnect in that case? – snakecharmerb Feb 03 '23 at 07:14
  • @snakecharmerb if you notice, I am already check `None` nd then calling `reconnect()`. – Volatil3 Feb 03 '23 at 07:15
  • Being closed and being `None` are different things. A connection object has an `is_closed` method that returns a bool. – snakecharmerb Feb 03 '23 at 07:18
  • @snakecharmerb got it, so if it returns `False`, will calling `connection.reconnect()` work here? or I should call `ping` method? – Volatil3 Feb 03 '23 at 07:23
  • Reconnecting should work assuming the problem is a transient issue like a timeout. – snakecharmerb Feb 03 '23 at 07:25
  • @snakecharmerb Alright, so calling `reconnect` is much better than creating a new connection Object? – Volatil3 Feb 03 '23 at 07:30
  • Does this answer your question? [cursor() raise errors.OperationalError("MySQL Connection not available.") OperationalError: MySQL Connection not available](https://stackoverflow.com/questions/27537892/cursor-raise-errors-operationalerrormysql-connection-not-available-operat) – Nicholas Hansen-Feruch Feb 03 '23 at 07:44
  • @NicholasHansen-Feruch No because I already had visited and implemented. I have done – Volatil3 Feb 03 '23 at 14:31
  • @snakecharmerb So I am having a weird. I am calling `connection.reconnect` if the `connection` variable is not `None` and `is_connected()` is `False` but I had enabled logging and I see `connection` variable is returning as `NULL` now. – Volatil3 Feb 04 '23 at 15:58

0 Answers0