Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/common.py
Show All 37 Lines | def decorator(meth, __client_options=client_options): | ||||
if 'cur' in kwargs and kwargs['cur']: | if 'cur' in kwargs and kwargs['cur']: | ||||
cur = kwargs['cur'] | cur = kwargs['cur'] | ||||
old_options = apply_options(cur, __client_options) | old_options = apply_options(cur, __client_options) | ||||
ret = meth(self, *args, **kwargs) | ret = meth(self, *args, **kwargs) | ||||
apply_options(cur, old_options) | apply_options(cur, old_options) | ||||
return ret | return ret | ||||
else: | else: | ||||
db = self.get_db() | db = self.get_db() | ||||
try: | |||||
with db.transaction() as cur: | with db.transaction() as cur: | ||||
apply_options(cur, __client_options) | apply_options(cur, __client_options) | ||||
return meth(self, *args, db=db, cur=cur, **kwargs) | return meth(self, *args, db=db, cur=cur, **kwargs) | ||||
finally: | |||||
olasd: Can we guard this with an `if hasattr(self, 'put_db')` to avoid making other users of this… | |||||
Done Inline ActionsNo, because I removed BaseDb.__del__, so it would leak connections in prod. vlorentz: No, because I removed `BaseDb.__del__`, so it would leak connections in prod. | |||||
self.put_db(db) | |||||
return _meth | return _meth | ||||
return decorator | return decorator | ||||
def db_transaction_generator(**client_options): | def db_transaction_generator(**client_options): | ||||
"""decorator to execute Backend methods within DB transactions, while | """decorator to execute Backend methods within DB transactions, while | ||||
returning a generator | returning a generator | ||||
Show All 11 Lines | def decorator(meth, __client_options=client_options): | ||||
def _meth(self, *args, **kwargs): | def _meth(self, *args, **kwargs): | ||||
if 'cur' in kwargs and kwargs['cur']: | if 'cur' in kwargs and kwargs['cur']: | ||||
cur = kwargs['cur'] | cur = kwargs['cur'] | ||||
old_options = apply_options(cur, __client_options) | old_options = apply_options(cur, __client_options) | ||||
yield from meth(self, *args, **kwargs) | yield from meth(self, *args, **kwargs) | ||||
apply_options(cur, old_options) | apply_options(cur, old_options) | ||||
else: | else: | ||||
db = self.get_db() | db = self.get_db() | ||||
try: | |||||
with db.transaction() as cur: | with db.transaction() as cur: | ||||
apply_options(cur, __client_options) | apply_options(cur, __client_options) | ||||
yield from meth(self, *args, db=db, cur=cur, **kwargs) | yield from meth(self, *args, db=db, cur=cur, **kwargs) | ||||
finally: | |||||
Not Done Inline ActionsSame comment olasd: Same comment | |||||
self.put_db(db) | |||||
return _meth | return _meth | ||||
return decorator | return decorator |
Can we guard this with an if hasattr(self, 'put_db') to avoid making other users of this (indexer, archiver) insta-buggy?