Skip to content

Commit

Permalink
Added DB reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko259 committed Feb 6, 2022
1 parent 34b0fb0 commit e1c53b4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@ def db_connection():

global _connection

# Return connection
if _connection is not None:

# Check if connection is still alive
try:
_connection.ping(reconnect=False, attempts=3, delay=5)
except mysql.connector.InterfaceError as err:
print("[MySQL] Connection lost. Reconnecting...")
_connection = init_connection()

return _connection
else:
_connection = init_connection()
return _connection

def init_connection():

global _connection

try:
_connection = mysql.connector.connect(
host=BOT_DB_HOST,
Expand All @@ -25,4 +41,4 @@ def db_connection():
return _connection

except Error as e:
print("[MySQL] Error while connecting: ", e)
print("[MySQL] Error while connecting: ", e)

0 comments on commit e1c53b4

Please sign in to comment.