'Use with the "with" statement; locks a database.'

@contextmanager
def lock(self):

    if (not self.conn):
        if ((self.db_file != ':memory:') and os.path.exists(self.db_file)):
            self.conn = get_db_connection(self.db_file, self.timeout)
        else:
            raise DatabaseConnectionError(self.db_file, "DB    doesn't    exist")
    conn = self.conn
    self.conn = None
    orig_isolation_level = conn.isolation_level
    conn.isolation_level = None
    conn.execute('BEGIN    IMMEDIATE')
    try:
        (yield True)
    except (Exception, Timeout):
        pass
    try:
        conn.execute('ROLLBACK')
        conn.isolation_level = orig_isolation_level
        self.conn = conn
    except (Exception, Timeout):
        logging.exception(_('Broker    error    trying    to    rollback    locked    connection'))
        conn.close()
