Improvement on SQL connections and timeouts.
This commit is contained in:
@@ -15,5 +15,5 @@ PyJWT
|
|||||||
pandas
|
pandas
|
||||||
openpyxl
|
openpyxl
|
||||||
xlrd
|
xlrd
|
||||||
qrcode
|
qrcode[pil]
|
||||||
# mypy
|
# mypy
|
||||||
|
|||||||
@@ -11,11 +11,16 @@ from sqlalchemy.exc import IntegrityError, OperationalError
|
|||||||
db_uri = settings.SQLALCHEMY_DB_URI
|
db_uri = settings.SQLALCHEMY_DB_URI
|
||||||
|
|
||||||
connection_string = db_uri
|
connection_string = db_uri
|
||||||
engine = create_engine(url=connection_string, pool_size=25, pool_recycle=60, pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ COMMITTED')
|
engine = create_engine(url=connection_string, pool_size=25, pool_recycle=60, pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ COMMITTED', connect_args={'connect_timeout': 30})
|
||||||
# NOTE: The default isolation_level is 'REPEATABLE READ'. This can sometimes not show updated data.
|
# NOTE: The default isolation_level is 'REPEATABLE READ'. This can sometimes not show updated data.
|
||||||
# NOTE: The "echo" set to True option shows the SQL queries.
|
# NOTE: The "echo" set to True option shows the SQL queries.
|
||||||
|
|
||||||
|
log.info('DB SQL trying to connect...')
|
||||||
|
try:
|
||||||
db = engine.connect()
|
db = engine.connect()
|
||||||
|
log.info(f'Connected to database: {db_uri}')
|
||||||
|
except:
|
||||||
|
log.exception('Could not connect to database.')
|
||||||
|
|
||||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
# log.debug(db)
|
# log.debug(db)
|
||||||
@@ -23,7 +28,7 @@ db = engine.connect()
|
|||||||
# log.debug(dir(db))
|
# log.debug(dir(db))
|
||||||
|
|
||||||
def sql_connect(current_db):
|
def sql_connect(current_db):
|
||||||
log.setLevel(logging.DEBUG)
|
log.setLevel(logging.INFO)
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
# log.info('Trying to recreate the pool...')
|
# log.info('Trying to recreate the pool...')
|
||||||
@@ -43,7 +48,7 @@ def sql_connect(current_db):
|
|||||||
DB = settings.DB
|
DB = settings.DB
|
||||||
settings.SQLALCHEMY_DB_URI = 'mysql://'+DB['username']+':'+DB['password']+'@'+DB['server']+'/'+DB['name']
|
settings.SQLALCHEMY_DB_URI = 'mysql://'+DB['username']+':'+DB['password']+'@'+DB['server']+'/'+DB['name']
|
||||||
|
|
||||||
new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, pool_size=25, pool_recycle=60, pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ COMMITTED')
|
new_engine = create_engine(url=settings.SQLALCHEMY_DB_URI, pool_size=25, pool_recycle=60, pool_pre_ping=True, echo=False, echo_pool=True, isolation_level='READ COMMITTED', connect_args={'connect_timeout': 30})
|
||||||
|
|
||||||
current_db.engine = new_engine
|
current_db.engine = new_engine
|
||||||
|
|
||||||
@@ -128,7 +133,6 @@ def sql_insert(
|
|||||||
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string});
|
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string});
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
# print(sql_insert)
|
|
||||||
|
|
||||||
log.debug(f"""
|
log.debug(f"""
|
||||||
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string});
|
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string});
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ print('### **** *** ** * The Aether FastAPI API is loading... * ** *** **** ###'
|
|||||||
|
|
||||||
|
|
||||||
#log = logging.getLogger('root')
|
#log = logging.getLogger('root')
|
||||||
#log.setLevel(logging.ERROR) # DEBUG > INFO > WARNING > ERROR > CRITICAL
|
# log.setLevel(logging.DEBUG) # DEBUG > INFO > WARNING > ERROR > CRITICAL
|
||||||
#logging.basicConfig(
|
#logging.basicConfig(
|
||||||
#format='[%(asctime)s] %(levelname)s @ %(module)s.%(funcName)s()#%(lineno)d: %(message)s'
|
#format='[%(asctime)s] %(levelname)s @ %(module)s.%(funcName)s()#%(lineno)d: %(message)s'
|
||||||
#)
|
#)
|
||||||
|
|||||||
Reference in New Issue
Block a user