cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How can i check if database is online?

Hello,

i am beginner on neo4j, but not on .net and c#.
On other databases i can open the connector and if database is offline throw the connector an exeption or i can test it with a attach-function.

How can i do it on neo4j?

My "testcode":

// test connect to Neo4j
using (IDriver dbDriver = GraphDatabase.Driver("neo4J://localhost:7687", AuthTokens.Basic("neo4j", "none")))
{
    // it is possible here to check is server reachable?
    using (IAsyncSession dbSession = dbDriver.AsyncSession(o => o.WithDatabase("mydatabase")))
    {       
       // or check it on this position?
        string query = @"MATCH (n) RETURN n";
        try
        { 
            var result = await dbSession.ReadTransactionAsync(async tx =>
            {
                var cursor = await tx.RunAsync(query);
                var fetched = await cursor.FetchAsync();
                var output = new List<object>();
                while (fetched)
                {
                    output.Add(cursor.Current["n"]);
                    fetched = await cursor.FetchAsync();
                }
                return output;
            });
            await dbSession.CloseAsync();
        }
        catch (Exception ex)
        {
            // only the ReadTransactionAsync throws exception
        }
    }
    await dbDriver.CloseAsync();
}

regards Mario

1 ACCEPTED SOLUTION

Charlotte just beat me to it If it is the DB that you want to check exists then you have to run something against it. If it is connectivity to the server that you want to test instead then there is a Driver.VerifyConnectivityAsync() method that you can use.

View solution in original post

3 REPLIES 3

Hi Mario,

The only way I've done this in the past is to run a simple RETURN 1 statement and use it as a test, or something like CALL dbms.components() to get the Server version etc

Something like:

await session.RunAsync("RETURN 1");

With a try/catch around it

Charlotte

Charlotte just beat me to it If it is the DB that you want to check exists then you have to run something against it. If it is connectivity to the server that you want to test instead then there is a Driver.VerifyConnectivityAsync() method that you can use.

Hi Charlotte and Andy,

thanks for your response.
I have test both variants and i use the Driver.VerifyConnectivityAsync().

Both spend ~3 seconds, but the dbDriver.VerifyConnectivityAsync() is calling before
create the session.

many thanks and i wich everyone a merry chistmas
Mario