3

I'm doing a microservice in Python 3.7 that connects to a Neo4j database. It's the first time I work connecting Python with Neo4j and I'm using py2neo version 4.3.0. Everything works OK, but now to adhere to the standard, I need to create a healthcheck to verify the connection to the Database. I wanted to use the

from py2neo import Graph, Database

and use

db = Database ("bolt: // localhost: 7474", auth = ("neo4j", "xxxx"))

and

db.kernel_version (Dont work)

but with this I do not verify that there is connection is up. Does anybody have any suggestions?

alejomarchan
  • 368
  • 1
  • 10
  • 20

1 Answers1

3

If checking the kernel version doesn't work then the connection is not ok. Below is a script to check if the connection from python to neo4j (via py2neo) is up and running.

from py2neo import Graph
graph = Graph("bolt://localhost:7687", auth=("neo4j", "xxxxx"))
try:
    graph.run("Match () Return 1 Limit 1")
    print('ok')
except Exception:
    print('not ok')
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
  • Thanks you very much AnonyXmous. When i try to use that (db.kernel_version), the program dont respond. It freezes, so I wanted to know if there was some other method. – alejomarchan Jun 21 '19 at 19:23
  • Then it is not connection to neo4j but your program. You can hack py2neo and put debug somewhere. If not working, try uninstall/install py2neo. – jose_bacoy Jun 21 '19 at 19:27
  • 1
    I already have a process that works. I connect in the following way `graph = Graph(db_str, auth=("neo4j", "xxxx"))` and execute the following query `graph.run("MATCH (roots:" + elementType + "{ID:'" + elementName + "'})-[*]->leaves return count(leaves)")` and all ok. Now I want to verify that this connection is up. With db.kernel_version I could not – alejomarchan Jun 21 '19 at 19:34
  • then you can run/execute a dummy query and if that query returns data, then your db connection is working well. See my updated answer. – jose_bacoy Jun 21 '19 at 19:48
  • You are a crack anonyXmouS! Thank you very much, it worked perfect! – alejomarchan Jun 21 '19 at 20:11
  • You're welc. Glad to help. Please accept my answer by clicking on the check button. Thanks. – jose_bacoy Jun 21 '19 at 20:14