C# Driver - Session.CloseAsync() not closing the connections effectively

194 Views Asked by At

I am using .NET Driver for Neo4j .

Environment: .NET 6 Neo4j Server version : 4.3.2 Driver version : Neo4j.Driver 4.4.0

We are using a singleton driver connection with the server using the following code snippet and reusing it across all the sessions.

Neo4j.Driver.IDriver _driver = GraphDatabase.Driver("neo4j://*.*.*.*:7687", AuthTokens.Basic("neo4j", "*****"));

And we are opening and closing a session with each transaction like

var session = _driver.AsyncSession(o => o.WithDatabase("pdb00"));
    try
    {
            return await session.ReadTransactionAsync(async tx =>
            {
                var result = await tx.RunAsync
                                         (
                                            query, parameters
                                          );

                res = await result.ToListAsync();
                var counters = await result.ConsumeAsync();

                Console.WriteLine("Time taken to read query " + index + ":  " + counters.ResultConsumedAfter.TotalMilliseconds);
                return res;

            });
        
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        throw;
    }
    finally
    {
        await session.CloseAsync();
    }

However when we monitor the number of active connections to the Neo4j server using the following command

call dbms.listConnections()

We are seeing as many connections as the number of sessions that are made, and the connections are not getting dropped until the Driver is closed.

For instance, if we call the transaction 100 times, the active connections increase by 100, and they are staying as-is even though session.closeasync() is getting invoked per session.

And only after Driver.closeAsync() is invoked at the application end, all the connections are dropped.

On heavy load, we are running into server overloading and port exhaustion scenarios due to this behavior.

Snapshot of current connections: Neo4j browser snapshot

Are we missing something here ?

Thanks in advance.

1

There are 1 best solutions below

2
Charlotte Skardon On

The driver maintains a connection pool, CloseAsync on a session doesn't dispose of the session, it just releases it back to the pool, so over time, you'll hit the max number.

I can't remember what the default is, but have you tried setting the MaxConnectionPoolSize on construction of the driver?

Neo4j.Driver.IDriver _driver = GraphDatabase.Driver(
    "neo4j://*.*.*.*:7687", 
    AuthTokens.Basic("neo4j", "*****")
    config => config.WithMaxConnectionPoolSize(10)
);

You could also play around with the other config elements:

Neo4j.Driver.IDriver _driver = GraphDatabase.Driver(
    "neo4j://*.*.*.*:7687", 
    AuthTokens.Basic("neo4j", "*****")
    config => config
        .WithMaxConnectionLifetime(TimeSpan.FromSeconds(10))
        .WithMaxConnectionPoolSize(10)
        .WithMaxIdleConnectionPoolSize(10)
);