Search code examples
pythonneo4jcypher

Get every running query from every sesssion using Neo4j and Python


I'm trying to log every active query which has been running for more than 2 minutes. But when I use,

running_queries = session.run(
            "CALL dbms.listQueries()"
        )

I have another script running in which there is an infinite loop calling a simple query in another session. But it only returns the queries that are running inside my session.


Solution

  • I did a quick experiment and cannot confirm your findings. For simplicity, I used the Neo4j browser (which under the hood uses the official JavaScript driver).

    First, I start a Neo4j server with APOC installed (I tried 4.4 and 5.2), I then open two tabs (http://localhost:7474/browser/ if you started the DBMS on your local machine). In the first tab I run

    CALL apoc.util.sleep(15000)  // sleep 15 seconds
    

    in the second tab I run (within the next 15 seconds)

    SHOW TRANSACTIONS  // CALL dbms.listQueries() has been deprecated
    

    show transcations result showing both queries and as you can see it shows both transactions. Since the queries were launched from different tabs, they were sent through different drivers. So this scenario is very similar to you having two scripts. If I had to guess why you don't see both queries, it may be because the queries you're firing off in the loop are too short. So you just miss the actual queries the moment you run CALL dbms.listQueries(). Notice that there is more stuff around the queries that is going on like networking and things.