47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
import mariadb
|
|
|
|
class DbManager():
|
|
__connection = None
|
|
__con_param = None
|
|
__dbmanager = None
|
|
|
|
def __new__(cls, dbconf = None):
|
|
|
|
if cls.__dbmanager is None:
|
|
cls.__dbmanager = super(DbManager, cls).__new__(cls)
|
|
cls.__dbmanager.__initializeConfig(dbconf)
|
|
|
|
return cls.__dbmanager
|
|
|
|
def getConnection(cls):
|
|
#print(f"DB Manager: {cls.__dbmanager}")
|
|
#print(f"DB Connection: {cls.__connection}")
|
|
try:
|
|
if not cls.__connection or not cls.__connection.ping():
|
|
cls.__failure_notified = False
|
|
cls.__connection = mariadb.connect(**cls.__con_param)
|
|
except mariadb.InterfaceError as e:
|
|
cls.__connection = mariadb.connect(**cls.__con_param)
|
|
print(f"DbManager Connection (INTERFACE ERROR): {e}..reconnecting...")
|
|
except mariadb.Error as e:
|
|
if '(110)' in str(e):
|
|
print(f"File: {__file__}\n Database connection timed out (Check connection parameters or server running): {e}")
|
|
elif '(138)' in str(e):
|
|
print(f"File: {__file__}\n Database connection timed out (Check connection parameters or server running - initial handshake): {e}")
|
|
else:
|
|
print(f"File: {__file__}\n Database connection error: {e}")
|
|
cls.__connection = None
|
|
|
|
return cls.__connection
|
|
|
|
def __initializeConfig(cls, dbconf):
|
|
cls.__con_param = { 'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'],
|
|
'port': int (dbconf['DB_PORT']), 'host': dbconf['DB_HOST'],
|
|
'database': dbconf['DB_NAME'], 'connect_timeout': 5, 'autocommit': True,
|
|
}
|
|
|
|
|
|
|
|
|
|
|