Parcourir la source

Changing to a scoped session, bc that's what we should be used for thread safety

Kirk Trombley il y a 6 ans
Parent
commit
6db385ec84
1 fichiers modifiés avec 5 ajouts et 2 suppressions
  1. 5 2
      src/db.py

+ 5 - 2
src/db.py

@@ -1,14 +1,14 @@
 from contextlib import contextmanager
 
 from sqlalchemy import create_engine
-from sqlalchemy.orm import sessionmaker
+from sqlalchemy.orm import sessionmaker, scoped_session
 
 from config import DB_FILE
 from command_system import ModelBase
 
 
 engine = create_engine("sqlite:///" + DB_FILE)
-Session = sessionmaker(bind=engine)
+Session = scoped_session(sessionmaker(bind=engine))
 
 
 def init_db():
@@ -23,6 +23,9 @@ def session_scope():
         yield session
         session.commit()
     except:
+        # TODO there is some worry that this would rollback things in other threads...
+        # we should probably find a more correct solution for managing the threaded
+        # db access, but the risk is fairly low at this point.
         session.rollback()
         raise
     finally: