HSB3119-22080292-daothivananh commited on
Commit ·
5219ca7
1
Parent(s): fa184e8
cdhsgvcdsgh
Browse files- database/database.py +8 -1
database/database.py
CHANGED
|
@@ -13,14 +13,21 @@ db_config = {
|
|
| 13 |
"pool_size": 5,
|
| 14 |
"ssl_disabled": False
|
| 15 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Tao pool ket noi
|
| 17 |
try:
|
| 18 |
connection_pool = mysql.connector.pooling.MySQLConnectionPool(**db_config)
|
| 19 |
print("[OK] Da khoi tao Pool ket noi MySQL thanh cong!")
|
| 20 |
except Exception as e:
|
| 21 |
-
print(f"[
|
| 22 |
|
| 23 |
def get_db_connection():
|
|
|
|
|
|
|
|
|
|
| 24 |
return connection_pool.get_connection()
|
| 25 |
|
| 26 |
def init_database():
|
|
|
|
| 13 |
"pool_size": 5,
|
| 14 |
"ssl_disabled": False
|
| 15 |
}
|
| 16 |
+
|
| 17 |
+
# 1. Khai báo biến trước để giữ chỗ, tránh lỗi "not defined"
|
| 18 |
+
connection_pool = None
|
| 19 |
+
|
| 20 |
# Tao pool ket noi
|
| 21 |
try:
|
| 22 |
connection_pool = mysql.connector.pooling.MySQLConnectionPool(**db_config)
|
| 23 |
print("[OK] Da khoi tao Pool ket noi MySQL thanh cong!")
|
| 24 |
except Exception as e:
|
| 25 |
+
print(f"\n[CẢNH BÁO ĐỎ] Loi khoi tao Pool MySQL: {e}\n")
|
| 26 |
|
| 27 |
def get_db_connection():
|
| 28 |
+
# 2. Chốt chặn an toàn: Báo lỗi rõ ràng thay vì sập
|
| 29 |
+
if connection_pool is None:
|
| 30 |
+
raise Exception("Mất kết nối Database. Biến connection_pool chưa được khởi tạo. Hãy kiểm tra lại thông tin kết nối Railway!")
|
| 31 |
return connection_pool.get_connection()
|
| 32 |
|
| 33 |
def init_database():
|