From e02af0c88454664e16fe75071ac6d28feb4db755 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Thu, 18 Nov 2021 13:06:08 +0100 Subject: [PATCH 1/3] Put sessionmaker code in a function --- src/cnaas_nms/db/session.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cnaas_nms/db/session.py b/src/cnaas_nms/db/session.py index 0d01be4e..e51331ee 100644 --- a/src/cnaas_nms/db/session.py +++ b/src/cnaas_nms/db/session.py @@ -32,15 +32,17 @@ def get_sqlalchemy_conn_str(**kwargs) -> str: ) -conn_str = get_sqlalchemy_conn_str() -engine = create_engine(conn_str, pool_size=50, max_overflow=50) -connection = engine.connect() -Session = sessionmaker(bind=engine) +def get_session(): + conn_str = get_sqlalchemy_conn_str() + engine = create_engine(conn_str, pool_size=50, max_overflow=50) + engine.connect() + Session = sessionmaker(bind=engine) + return Session() @contextmanager def sqla_session(**kwargs): - session = Session() + session = get_session() try: yield session session.commit() From a71ceaba6d97fea5b5c12462f97524347d8e7d98 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Wed, 24 Nov 2021 13:43:33 +0100 Subject: [PATCH 2/3] Create sessionmaker once --- src/cnaas_nms/db/session.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cnaas_nms/db/session.py b/src/cnaas_nms/db/session.py index e51331ee..1aa21d4d 100644 --- a/src/cnaas_nms/db/session.py +++ b/src/cnaas_nms/db/session.py @@ -31,12 +31,14 @@ def get_sqlalchemy_conn_str(**kwargs) -> str: f"{db_data['hostname']}:{db_data['port']}/{db_data['database']}" ) - +Session = None def get_session(): - conn_str = get_sqlalchemy_conn_str() - engine = create_engine(conn_str, pool_size=50, max_overflow=50) - engine.connect() - Session = sessionmaker(bind=engine) + global Session + if Session is None: + conn_str = get_sqlalchemy_conn_str() + engine = create_engine(conn_str, pool_size=50, max_overflow=50) + engine.connect() + Session = sessionmaker(bind=engine) return Session() From 8c8f90e5fa1ad40d0237e71891ad1242e54c3e86 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Mon, 29 Nov 2021 14:34:13 +0100 Subject: [PATCH 3/3] Make code more pythonic --- src/cnaas_nms/db/session.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cnaas_nms/db/session.py b/src/cnaas_nms/db/session.py index 1aa21d4d..75f3ca37 100644 --- a/src/cnaas_nms/db/session.py +++ b/src/cnaas_nms/db/session.py @@ -7,6 +7,7 @@ from sqlalchemy.pool import NullPool from redis import StrictRedis +_sessionmaker = None def get_dbdata(config='/etc/cnaas-nms/db_config.yml'): with open(config, 'r') as db_file: @@ -31,20 +32,19 @@ def get_sqlalchemy_conn_str(**kwargs) -> str: f"{db_data['hostname']}:{db_data['port']}/{db_data['database']}" ) -Session = None -def get_session(): - global Session - if Session is None: +def _get_session(): + global _sessionmaker + if _sessionmaker is None: conn_str = get_sqlalchemy_conn_str() engine = create_engine(conn_str, pool_size=50, max_overflow=50) engine.connect() - Session = sessionmaker(bind=engine) - return Session() + _sessionmaker = sessionmaker(bind=engine) + return _sessionmaker() @contextmanager def sqla_session(**kwargs): - session = get_session() + session = _get_session() try: yield session session.commit()