further improve db error handling

This commit is contained in:
David Rodenkirchen
2024-08-29 12:05:04 +02:00
parent ac805e96da
commit dc514895df
4 changed files with 84 additions and 68 deletions
+53 -31
View File
@@ -33,10 +33,31 @@ class DatabaseService:
f"Connecting to database '{self._database_config.db_name}' on "
f"{self._database_config.db_user}@{self._database_config.db_host}:{self._database_config.db_port}"
)
self._establish_new_connection()
self._connection: Optional[mariadb.Connection] = None
self._reestablishment_lock = False
self.establish_new_connection()
@property
def is_connected(self) -> bool:
try:
self._connection.ping()
except Exception:
try:
self.establish_new_connection()
return True
except NoDatabaseConnectionError:
return False
return True
def establish_new_connection(self) -> None:
if self._reestablishment_lock:
return
self._reestablishment_lock = True
if isinstance(self._connection, mariadb.Connection):
self._connection.close()
self._connection = None
def _establish_new_connection(self) -> None:
retries = 0
for _ in range(DatabaseService.MAX_CONNECTION_RETRIES):
try:
self._connection = mariadb.connect(
@@ -47,10 +68,11 @@ class DatabaseService:
database=self._database_config.db_name
)
except mariadb.Error:
sleep(0.5)
retries += 1
sleep(0.4)
continue
self._reestablishment_lock = False
return
self._reestablishment_lock = False
raise NoDatabaseConnectionError
@@ -110,7 +132,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.create_user(user_name, user_mail, password_hash)
except mariadb.IntegrityError as e:
logger.warning(f"Aborted duplication entry: {e}")
@@ -130,7 +152,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.update_user(user)
except mariadb.IntegrityError as e:
logger.warning(f"Aborted duplication entry: {e}")
@@ -147,7 +169,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.add_transaction(transaction)
except Exception as e:
logger.warning(f"Error adding Transaction: {e}")
@@ -164,7 +186,7 @@ class DatabaseService:
self._connection.commit()
result = cursor.fetchall()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_all_transactions_for_user(user_id)
except mariadb.Error as e:
logger.error(f"Error getting all transactions for user: {e}")
@@ -190,7 +212,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.add_news(news)
except Exception as e:
logger.warning(f"Error adding Transaction: {e}")
@@ -202,7 +224,7 @@ class DatabaseService:
cursor.execute("SELECT * FROM news INNER JOIN users ON news.news_author = users.user_id WHERE news_date BETWEEN ? AND ?;", (dt_start, dt_end))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_news(dt_start, dt_end)
except Exception as e:
logger.warning(f"Error fetching news: {e}")
@@ -228,7 +250,7 @@ class DatabaseService:
cursor.execute("SELECT * FROM tickets INNER JOIN users ON tickets.user = users.user_id;", ())
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_tickets()
except Exception as e:
logger.warning(f"Error fetching tickets: {e}")
@@ -251,7 +273,7 @@ class DatabaseService:
cursor.execute("SELECT * FROM tickets INNER JOIN users ON tickets.user = users.user_id WHERE user_id=?;", (user_id, ))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_ticket_for_user(user_id)
except Exception as e:
logger.warning(f"Error fetching ticket for user: {e}")
@@ -275,7 +297,7 @@ class DatabaseService:
cursor.execute("INSERT INTO tickets (ticket_category, user) VALUES (?, ?)", (category, user_id))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.generate_ticket_for_user(user_id, category)
except Exception as e:
logger.warning(f"Error generating ticket for user: {e}")
@@ -290,7 +312,7 @@ class DatabaseService:
affected_rows = cursor.rowcount
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.change_ticket_owner(ticket_id, new_owner_id)
except Exception as e:
logger.warning(f"Error transferring ticket to user: {e}")
@@ -303,7 +325,7 @@ class DatabaseService:
cursor.execute("DELETE FROM tickets WHERE ticket_id = ?;", (ticket_id, ))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.change_ticket_owner(ticket_id)
except Exception as e:
logger.warning(f"Error deleting ticket: {e}")
@@ -319,7 +341,7 @@ class DatabaseService:
cursor.execute("INSERT INTO seats (seat_id, seat_category) VALUES (?, ?);", (seat[0], seat[1]))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.generate_fresh_seats_table(seats)
except Exception as e:
logger.warning(f"Error generating fresh seats table: {e}")
@@ -332,7 +354,7 @@ class DatabaseService:
cursor.execute("SELECT seats.*, users.* FROM seats LEFT JOIN users ON seats.user = users.user_id;")
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_seating_info()
except Exception as e:
logger.warning(f"Error getting seats table: {e}")
@@ -355,7 +377,7 @@ class DatabaseService:
affected_rows = cursor.rowcount
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.seat_user(seat_id, user_id)
except Exception as e:
logger.warning(f"Error seating user: {e}")
@@ -369,7 +391,7 @@ class DatabaseService:
cursor.execute("SELECT * FROM catering_menu_items;")
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_menu_items()
except Exception as e:
logger.warning(f"Error fetching menu items: {e}")
@@ -393,7 +415,7 @@ class DatabaseService:
cursor.execute("SELECT * FROM catering_menu_items WHERE catering_menu_item_id = ?;", (menu_item_id, ))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_menu_item(menu_item_id)
except Exception as e:
logger.warning(f"Error fetching menu items: {e}")
@@ -420,7 +442,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.add_menu_item(name, info, price, category, is_disabled)
except Exception as e:
logger.warning(f"Error adding menu item: {e}")
@@ -441,7 +463,7 @@ class DatabaseService:
cursor.execute("DELETE FROM catering_menu_items WHERE catering_menu_item_id = ?;", (menu_item_id,))
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.delete_menu_item(menu_item_id)
except Exception as e:
logger.warning(f"Error deleting menu item: {e}")
@@ -458,7 +480,7 @@ class DatabaseService:
affected_rows = cursor.rowcount
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.update_menu_item(updated_item)
except Exception as e:
logger.warning(f"Error updating menu item: {e}")
@@ -489,7 +511,7 @@ class DatabaseService:
is_delivery=is_delivery
)
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.add_new_order(menu_items, user_id, is_delivery)
except Exception as e:
logger.warning(f"Error placing order: {e}")
@@ -505,7 +527,7 @@ class DatabaseService:
affected_rows = cursor.rowcount
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.change_order_status(order_id, status)
except Exception as e:
logger.warning(f"Error updating menu item: {e}")
@@ -528,7 +550,7 @@ class DatabaseService:
cursor.execute(query)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_orders(user_id, status)
except Exception as e:
logger.warning(f"Error getting orders: {e}")
@@ -560,7 +582,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_menu_items_for_order(order_id)
except Exception as e:
logger.warning(f"Error getting order items: {e}")
@@ -587,7 +609,7 @@ class DatabaseService:
)
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.set_user_profile_picture(user_id, picture_data)
except Exception as e:
logger.warning(f"Error setting user profile picture: {e}")
@@ -602,7 +624,7 @@ class DatabaseService:
return
return r[0]
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_user_profile_picture(user_id)
except Exception as e:
logger.warning(f"Error setting user profile picture: {e}")
@@ -615,7 +637,7 @@ class DatabaseService:
cursor.execute("SELECT * FROM users;")
self._connection.commit()
except mariadb.InterfaceError:
self._establish_new_connection()
self.establish_new_connection()
return self.get_all_users()
except Exception as e:
logger.warning(f"Error getting all users: {e}")