from contextlib import contextmanager


@contextmanager
def get_connection(factory):
    connection = factory()
    try:
        yield connection
        connection.commit()
    finally:
        connection.close()
