'Context manager that ensures that a connection is established, and
if it opened one, closes it to avoid leaving a dangling connection.
This is useful for operations outside of the request-response cycle.
Provide a cursor: with self.temporary_connection() as cursor: ...'

@contextmanager
def temporary_connection(self):

    must_close = (self.connection is None)
    cursor = self.cursor()
    try:
        (yield cursor)
    finally:
        cursor.close()
        if must_close:
            self.close()
