def _uses_db(func, self, *args, **kwargs):
    """ Use as a decorator for operations on the database, to ensure connection setup and
    teardown. Can only be used on methods on objects with a `self.session` attribute.
    """
    if not self.session:
        _logger.debug('Creating new db session')
        self._init_db_session()
    try:
        ret = func(self, *args, **kwargs)
        self.session.commit()
    except:
        self.session.rollback()
        tb = traceback.format_exc()
        _logger.debug(tb)
        raise
    finally:
        _logger.debug('Closing db session')
        self.session.close()
    return ret
Use as a decorator for operations on the database, to ensure connection setup and
    teardown. Can only be used on methods on objects with a `self.session` attribute.