Improvement on SQL connections and timeouts.
This commit is contained in:
@@ -11,11 +11,16 @@ from sqlalchemy.exc import IntegrityError, OperationalError
|
||||
db_uri = settings.SQLALCHEMY_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 "echo" set to True option shows the SQL queries.
|
||||
|
||||
db = engine.connect()
|
||||
log.info('DB SQL trying to connect...')
|
||||
try:
|
||||
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.debug(db)
|
||||
@@ -23,7 +28,7 @@ db = engine.connect()
|
||||
# log.debug(dir(db))
|
||||
|
||||
def sql_connect(current_db):
|
||||
log.setLevel(logging.DEBUG)
|
||||
log.setLevel(logging.INFO)
|
||||
log.debug(locals())
|
||||
|
||||
# log.info('Trying to recreate the pool...')
|
||||
@@ -43,7 +48,7 @@ def sql_connect(current_db):
|
||||
DB = settings.DB
|
||||
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
|
||||
|
||||
@@ -128,7 +133,6 @@ def sql_insert(
|
||||
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string});
|
||||
"""
|
||||
)
|
||||
# print(sql_insert)
|
||||
|
||||
log.debug(f"""
|
||||
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string});
|
||||
|
||||
Reference in New Issue
Block a user