neo4j-examples/movies-python-bolt

Tearing down the driver

john-bodley opened this issue · 1 comments

Given that in the latest version of the neo4j-driver the driver is actually a context library which needs to be closed and thus when I terminate the app I get the following errors:

Exception TypeError: TypeError("'NoneType' object is not callable",) in <bound method DirectDriver.__del__ of <neo4j.v1.direct.DirectDriver object at 0x11203e210>> ignored
Exception TypeError: "'NoneType' object is not callable" in <bound method Connection.__del__ of <neo4j.bolt.connection.Connection object at 0x11203ed90>> ignored

Is the following the correct logic one should apply?

def get_driver():
    driver = getattr(g, 'driver', None)

    if driver is None:
        driver = g.driver = GraphDatabase.driver('bolt://localhost:7687')

    return driver

def get_session():
    session = getattr(g, 'session', None)

    if session is None:
        session = g.session = get_driver().session()

    return session


@app.teardown_appcontext
def teardown(exception):
    session = getattr(g, 'session', None)

    if session is not None:
        session.close()

    driver = getattr(g, 'driver', None)

    if driver is not None:
        driver.close()

An alternative approach would be to keep the driver globally defined and close it via,

import atexit

driver = GraphDatabase.driver('bolt://localhost')
atexit.register(lambda driver=driver: driver.close())

Things have changed since you opened this issue.
Feel free to reopen if you still encounter a similar problem.