I'm a bit new to sqlalchemy but don't see how I might do this.
Specifically, I need to acquire a lock before any database transactions to preserve integrity of a sqlite3 database stored on an NFS system. I know it sounds silly to want such a thing, but I use sqlite3 for development of a job management system that runs on a server farm and can't set up other resources without some overhead.
I was hoping that something like:
import sqlalchemy as sqa
engine = sqa.create_engine('sqlite...')
Session = sqa.orm.sessionmaker(bind=engine)
locking = True
lock = ... # a flufl file lock
class MySession(Session):
def execute(self, *args, **kwargs):
if locking:
with lock:
Session.execute(self, *args, **kwargs)
else:
Session.execute(self, *args, **kwargs)
But this doesn't work because Session isn't actually a class itself. There is an sqlalchemy.orm.session.Session class but I'm not clear on how to subclass that and have sessionmaker actually use it. Nor am I sure that surrounding things in this way will capture all transactions happening in objects returned from session.query() etc. (though I think it will since those modifications require session.commit() to be pushed to the database).
There might be other places in sqlalchemy that I should plug into such as the connection classes to do this, so open to those answers as well. Just want to make sure that nothing touches the sqlite3 database without first getting the lock.
Thanks Kyle
Aucun commentaire:
Enregistrer un commentaire