From 4820fba2ae235932d4e8333b8c05608263e271cb Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 26 Mar 2018 18:01:38 +0000 Subject: [PATCH] Use scoped session --- discordbot/titanembeds/database/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index eb3af33..e149cd9 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -1,7 +1,7 @@ from contextlib import contextmanager import sqlalchemy as db from sqlalchemy.engine import Engine, create_engine -from sqlalchemy.orm import sessionmaker, Session +from sqlalchemy.orm import sessionmaker, Session, scoped_session from sqlalchemy.ext.declarative import declarative_base import json @@ -24,21 +24,23 @@ class DatabaseInterface(object): self.engine = None # type: Engine self._sessionmaker = None # type: sessionmaker + self.session = None def connect(self, dburi): self.engine = create_engine(dburi, pool_recycle=10) @contextmanager def get_session(self): - Session = sessionmaker(bind=self.engine) + SessionMaker = sessionmaker(bind=self.engine) + Session = scoped_session(SessionMaker) session = Session() try: yield session - session.commit() except: session.rollback() finally: - session.close() + session.commit() + Session.remove() async def push_message(self, message): if message.guild: