I am using a single Neo4J driver object (in a Jersey Binder class as an instance of a singleton). The driver is created like this:
Config config = Config.builder()
.withMaxConnectionLifetime( 30, TimeUnit.MINUTES )
.withMaxConnectionPoolSize( 50 )
.withConnectionAcquisitionTimeout( 30, TimeUnit.SECONDS )
.withDriverMetrics()
.build();
this.driver = GraphDatabase.driver(String.format("neo4j://%s:%s",
"localhost", "port no",
AuthTokens.basic("username",
"password", config);
this.driver.verifyConnectivity();
this.logger.info("neo4j driver created {}", this.driver.metrics().connectionPoolMetrics());
But it seems like every time I am using this method connections do pile up
private Driver getDriver() {
if (this.driver == null) {
throw new RuntimeException("No Neo4J driver. Something wrong with connection or DB?");
}
this.logger.debug("getDriver metrics neo4j {}", this.driver.metrics().connectionPoolMetrics());
return this.driver;
}
// in public Session getSession();
Session session = null;
try {
session = this.getSession();
// do stuff
} catch (Exception ex) {
// stuff
// either
session.readTransaction( /* TransactionWork */ );
// or
Transaction tx = session.beginTransaction();
tx.run("wonderfull query", Map.of("field", "value"));
tx.commit();
tx.close();
} finally {
if (session != null) {
session.close();
}
}
until we have 50 connections and we cannot create more and get
org.neo4j.driver.exceptions.ClientException: Unable to acquire connection from the pool within configured maximum time of 30000ms
13:37:33 [ajp-nio-127.0.0.1-8009-exec-3] DEBUG dbConnection.Neo4JDriver - getDriver metrics neo4j [localhost:7687-138669777=[
created=50,
closed=0,
creating=0,
failedToCreate=0,
acquiring=1,
acquired=120,
timedOutToAcquire=0,
inUse=50,
idle=0,
// ...
totalInUseCount=70],
localhost:7687-571435580=[created=1,
closed=0,
creating=0,
failedToCreate=0,
acquiring=0,
acquired=59,
timedOutToAcquire=0,
inUse=1,
idle=0,
// ...
totalInUseCount=58]]
13:38:03 [ajp-nio-127.0.0.1-8009-exec-2] WARN restResources.SomeEndpointCollection - Exception in myMethod Unable to acquire connection from the pool within configured maximum time of 30000ms
org.neo4j.driver.exceptions.ClientException: Unable to acquire connection from the pool within configured maximum time of 30000ms
at org.neo4j.driver.internal.util.Futures.blockingGet(Futures.java:111) ~[neo4j-java-driver-4.4.9.jar:4.4.9-e855bcc800deff6ddcf064c822314bb5c8d08c53]
at org.neo4j.driver.internal.InternalSession.beginTransaction(InternalSession.java:90) ~[neo4j-java-driver-4.4.9.jar:4.4.9-e855bcc800deff6ddcf064c822314bb5c8d08c53]
at org.neo4j.driver.internal.InternalSession.beginTransaction(InternalSession.java:85) ~[neo4j-java-driver-4.4.9.jar:4.4.9-e855bcc800deff6ddcf064c822314bb5c8d08c53]
So I think I am doing something fundamentally wrong (for two years now!). How can I manage connections effectively? I have already learned that I am not supposed to close the driver, because it should be a single instance for the whole lifetime of the application. But why should I let it create more and more connections?
You may have a look at this SO-question.
Maybe there is one problem: the connection is not closed when I do not retrieve/consume the results? Usually I need the results from the queries (of course), but in rare cases I don't. Should I check the code and make sure that I always consume the results (in TransactionWork or after tx.run())?
Any other hint?
I can increase the number of max-connection to 300 or 3000, but why would I need such a large number of connections unless I have to fire up queries, which need a large amount of time?
PS: I am wondering what is a Session? Does it represent a connection?
From the docs:
The session lifetime extends from session construction to session closure. In languages that support them, simple sessions are usually scoped within a context block; this ensures that they are properly closed and that any underlying connections are released and not leaked.
But what does closing a session actually mean? Does it just mean that you cannot perform any more actions with it?