Trying to fix Lost connection to MySQL server during query
This commit is contained in:
50
bot.py
50
bot.py
@@ -105,20 +105,25 @@ db_cursor = db_connection.cursor()
|
|||||||
def close_database_connection(connection):
|
def close_database_connection(connection):
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, nickname, profile_picture, join_date, xp=0, level=1):
|
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, xp=0, level=1, nickname="", profile_picture="", join_date=None, leave_date=None):
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, nickname, profile_picture, join_date, xp, level, leave_date)
|
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, xp, level, nickname, profile_picture, join_date, leave_date)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
"""
|
"""
|
||||||
serialized_chat_history = json.dumps(chat_history)
|
serialized_chat_history = json.dumps(chat_history)
|
||||||
data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, nickname, profile_picture, join_date, xp, level, None)
|
data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, xp, level, nickname, profile_picture, join_date, leave_date)
|
||||||
try:
|
|
||||||
db_cursor.execute(insert_query, data)
|
def execute_insert():
|
||||||
|
cursor = get_database_cursor()
|
||||||
|
cursor.execute(insert_query, data)
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
|
|
||||||
|
try:
|
||||||
|
retry_query(execute_insert)
|
||||||
print("User data inserted successfully.")
|
print("User data inserted successfully.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error inserting user data: {e}")
|
print(f"Error inserting user data after retries: {e}")
|
||||||
db_connection.rollback()
|
|
||||||
|
|
||||||
def update_user_data(user_id, guild_id, field, value):
|
def update_user_data(user_id, guild_id, field, value):
|
||||||
global db_connection, db_cursor # global-Deklaration muss vor dem Zugriff erfolgen
|
global db_connection, db_cursor # global-Deklaration muss vor dem Zugriff erfolgen
|
||||||
@@ -149,13 +154,42 @@ def update_user_data(user_id, guild_id, field, value):
|
|||||||
|
|
||||||
|
|
||||||
def connect_to_database():
|
def connect_to_database():
|
||||||
return mysql.connector.connect(
|
connection = mysql.connector.connect(
|
||||||
host=DB_HOST,
|
host=DB_HOST,
|
||||||
port=DB_PORT,
|
port=DB_PORT,
|
||||||
user=DB_USER,
|
user=DB_USER,
|
||||||
password=DB_PASSWORD,
|
password=DB_PASSWORD,
|
||||||
database=DB_DATABASE
|
database=DB_DATABASE
|
||||||
)
|
)
|
||||||
|
connection.autocommit = True # Automatisches Commit für stabilere Abfragen
|
||||||
|
return connection
|
||||||
|
|
||||||
|
def retry_query(func, *args, retries=3, delay=5):
|
||||||
|
for _ in range(retries):
|
||||||
|
try:
|
||||||
|
return func(*args)
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
print(f"Retrying due to error: {err}")
|
||||||
|
time.sleep(delay)
|
||||||
|
raise RuntimeError("Max retries exceeded")
|
||||||
|
|
||||||
|
def get_database_cursor():
|
||||||
|
if not db_connection.is_connected():
|
||||||
|
db_connection.reconnect(attempts=3, delay=5)
|
||||||
|
return db_connection.cursor()
|
||||||
|
|
||||||
|
pool = mysql.connector.pooling.MySQLConnectionPool(
|
||||||
|
pool_name="mypool",
|
||||||
|
pool_size=10,
|
||||||
|
host=DB_HOST,
|
||||||
|
port=DB_PORT,
|
||||||
|
user=DB_USER,
|
||||||
|
password=DB_PASSWORD,
|
||||||
|
database=DB_DATABASE
|
||||||
|
)
|
||||||
|
|
||||||
|
def connect_to_database():
|
||||||
|
return pool.get_connection()
|
||||||
|
|
||||||
def close_database_connection(connection):
|
def close_database_connection(connection):
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user