I’m using the new RDF4J bindings to connect to a Stardog server. I was able to establish connections and read/write data to it. But after several reads/writes I started getting an error where the connection to the Stardog server would freeze. After restarting my VM (not the Stardog server) I was able to connect again. But after the same amount of reads/writes I got the same problem.
I started to debug the code to see if resources weren’t being properly closed and I found out that the PoolHttpClientConnectionManager
was not releasing several connections created for construct queries. After digging a little bit more I found out that the reason they were not being released was because the response entity wasn’t being closed. I narrowed the problem to the getStatements
method of StardogRepositoryConnection
. That method returns an iterator that is wrapping a Stream<Statement>
that is directly attached to the response entity of the connection. But the iterator returned doesn’t close that stream unless all of the statements are consumed.
Steps to reproduce
// ... initialization code
RepositoryConnection connection = stardogRepository.getConnection();
boolean statementsExist = connection.hasStatement( someSubject, somePredicate, someObject, inferred, someContext );
// After the last statement gets executed an HttpConnection will have been created and executed but not released
// This is because `hasStatement` uses `getStatements` but only calls `hasNext` on the results and then closes the iteration
// but the iteration doesn't close the stream it is wrapping
// ... continue code
Workaround
// ... initialization code
RepositoryConnection connection = stardogRepository.getConnection();
boolean statementsExist = false;
try ( RepositoryResult<Statement> statements = getStatements( someSubject, somePredicate, someObject, inferred, someContext ) ) {
while( statements.hasNext() ) {
// The returned iterator will close the internal Stream<Statement> once we ask for the last element
statements.next();
statementsExist = true;
}
}
// ... continue code
Environment
Stardog: 5.0.3
Stardog/RDF4J bindings: com.complexible.stardog.rdf4j:stardog-rdf4j:5.0.3