def METHOD_NAME(self, function, *args, **kwargs):
    """
    Wrap provided function calling it inside a thread and
    passing the ORM session to it.
    """
    global THREAD_LOCAL
    session = getattr(THREAD_LOCAL, 'session', None)
    if not session:
        session = THREAD_LOCAL.session = get_session()
    retries = 0
    try:
        while True:
            try:
                if self.instance:
                    result = function(self.instance, session, *args, **kwargs)
                else:
                    result = function(session, *args, **kwargs)
                session.commit()
            except OperationalError as e:
                session.rollback()
                if "database is locked" not in str(e):
                    raise
                retries += 1
                if retries >= _ORM_TRANSACTION_RETRIES:
                    raise Exception("Transaction failed with too many retries")
                time.sleep(0.2 * random.uniform(1, 2 ** retries))
            except:
                session.rollback()
                raise
            else:
                return result
    finally:
        session.close()