Trying to make the SQL connections more reliable with threads.

This commit is contained in:
Scott Idem
2024-10-08 18:45:53 -04:00
parent 50955aff3a
commit 30c39d58dd

View File

@@ -7,6 +7,7 @@ from app.log import log, logging, logger_reset
from sqlalchemy import create_engine, text, Time from sqlalchemy import create_engine, text, Time
from sqlalchemy.exc import IntegrityError, OperationalError, ProgrammingError from sqlalchemy.exc import IntegrityError, OperationalError, ProgrammingError
from sqlalchemy.pool import NullPool from sqlalchemy.pool import NullPool
# from multiprocessing import Pool
db_uri = settings.SQLALCHEMY_DB_URI db_uri = settings.SQLALCHEMY_DB_URI
@@ -31,6 +32,22 @@ engine = create_engine(
# NOTE: Switching from READ COMMITTED to READ UNCOMMITTED (or REPEATABLE READ). Testing started 2024-04-23 # NOTE: Switching from READ COMMITTED to READ UNCOMMITTED (or REPEATABLE READ). Testing started 2024-04-23
# levels: "REPEATABLE READ" "READ COMMITTED" "READ UNCOMMITTED" "SERIALIZABLE" # levels: "REPEATABLE READ" "READ COMMITTED" "READ UNCOMMITTED" "SERIALIZABLE"
# def run_in_process(some_data_record):
# with engine.connect() as conn:
# conn.execute(text("..."))
# def initializer():
# """ensure the parent proc's database connections are not touched
# in the new connection pool"""
# engine.dispose(close=False)
# with Pool(10, initializer=initializer) as p:
# p.map(run_in_process, data)
log.info('DB SQL trying to connect...') log.info('DB SQL trying to connect...')
db = None db = None
try: try:
@@ -1268,7 +1285,7 @@ def redis_lookup_id_random(
table_name: str, table_name: str,
check_int_id: bool = False, check_int_id: bool = False,
log_lvl: int = logging.INFO, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log_lvl: int = logging.INFO, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
minutes: int = 8, # Expire the Redis key after 8 minutes minutes: int = 30, # Expire the Redis key after 8 minutes
reset_rate: int = 10, # 1 in 10 chance of resetting the Redis key reset_rate: int = 10, # 1 in 10 chance of resetting the Redis key
) -> str|int|bool|None: ) -> str|int|bool|None:
log.setLevel(log_lvl) log.setLevel(log_lvl)