From 0e7dfa443eecdbba134d2cc2d53fdf96a4df61cb Mon Sep 17 00:00:00 2001 From: Keiran Price Date: Mon, 29 Jul 2019 09:28:10 +0100 Subject: [PATCH] Create SessionManager --- common/database_helpers.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common/database_helpers.py b/common/database_helpers.py index 04316bec..a8366cbb 100644 --- a/common/database_helpers.py +++ b/common/database_helpers.py @@ -11,16 +11,21 @@ log = logging.getLogger() +class SessionManager(object): + _session = None + + @staticmethod def get_icat_db_session(): """ - Gets a session and connects with the ICAT database - :return: the session object + Checks if a session exists, if it does it returns the session if not a new one is created + :return: ICAT DB session """ log.info(" Getting ICAT DB session") + if SessionManager._session is None: engine = create_engine(Constants.DATABASE_URL) Session = sessionmaker(bind=engine) - session = Session() - return session + SessionManager._session = Session() + return SessionManager._session class Query(ABC):