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)