Skip to content

Commit

Permalink
Keep the close commands previously removed when replacing dataset wra…
Browse files Browse the repository at this point in the history
…pper with dataset.

The previous code is only closing the connection when the SDB has a connection.
This makes no sense for TDB and unless TDB is requiring a functioning SDB then this is a no-op command.

This seems wrong.
I removed the close entirely in the previous commit based on this observation.
However, I suspect that the SDB connection might still be being made even when using TDB.
Put the close back.
It also makes more sense to me to properly close things.
  • Loading branch information
kaladay committed Feb 28, 2023
1 parent a842da8 commit 7215809
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(String subject

} finally {
dataset.getLock().leaveCriticalSection();
dataset.close();
if (qexec != null) {
qexec.close();
}
Expand Down Expand Up @@ -454,6 +455,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(
return Collections.emptyList();
} finally {
dataset.getLock().leaveCriticalSection();
dataset.close();
if (qexec != null) {
qexec.close();
}
Expand Down Expand Up @@ -504,6 +506,7 @@ private Model constructModelForSelectQueries(String subjectUri,
qe.close();
}
dataset.getLock().leaveCriticalSection();
dataset.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,28 +397,36 @@ private Model parseModel(ModelChange modelChange) {
private InputStream getRDFResultStream(String query, boolean construct,
ModelSerializationFormat resultFormat) throws RDFServiceException {
Dataset d = getDataset();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
ByteArrayOutputStream serializedModel = new ByteArrayOutputStream();
try {
// TODO pipe this
Model m = construct ? qe.execConstruct() : qe.execDescribe();
m.write(serializedModel, getSerializationFormatString(resultFormat));
InputStream result = new ByteArrayInputStream(serializedModel.toByteArray());
return result;
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
ByteArrayOutputStream serializedModel = new ByteArrayOutputStream();
try {
// TODO pipe this
Model m = construct ? qe.execConstruct() : qe.execDescribe();
m.write(serializedModel, getSerializationFormatString(resultFormat));
InputStream result = new ByteArrayInputStream(serializedModel.toByteArray());
return result;
} finally {
qe.close();
}
} finally {
qe.close();
d.close();
}
}

private void getRDFModel(String query, boolean construct, Model model) throws RDFServiceException {
Dataset d = getDataset();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
Model m = construct ? qe.execConstruct(model) : qe.execDescribe(model);
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
Model m = construct ? qe.execConstruct(model) : qe.execDescribe(model);
} finally {
qe.close();
}
} finally {
qe.close();
d.close();
}
}

Expand Down Expand Up @@ -449,56 +457,68 @@ public InputStream sparqlDescribeQuery(String query,
public InputStream sparqlSelectQuery(String query, ResultFormat resultFormat)
throws RDFServiceException {
Dataset d = getDataset();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
ResultSet resultSet = qe.execSelect();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
switch (resultFormat) {
case CSV:
ResultSetFormatter.outputAsCSV(outputStream,resultSet);
break;
case TEXT:
ResultSetFormatter.out(outputStream,resultSet);
break;
case JSON:
ResultSetFormatter.outputAsJSON(outputStream, resultSet);
break;
case XML:
ResultSetFormatter.outputAsXML(outputStream, resultSet);
break;
default:
throw new RDFServiceException("unrecognized result format");
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
ResultSet resultSet = qe.execSelect();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
switch (resultFormat) {
case CSV:
ResultSetFormatter.outputAsCSV(outputStream,resultSet);
break;
case TEXT:
ResultSetFormatter.out(outputStream,resultSet);
break;
case JSON:
ResultSetFormatter.outputAsJSON(outputStream, resultSet);
break;
case XML:
ResultSetFormatter.outputAsXML(outputStream, resultSet);
break;
default:
throw new RDFServiceException("unrecognized result format");
}
InputStream result = new ByteArrayInputStream(outputStream.toByteArray());
return result;
} finally {
qe.close();
}
InputStream result = new ByteArrayInputStream(outputStream.toByteArray());
return result;
} finally {
qe.close();
d.close();
}
}

@Override
public void sparqlSelectQuery(String query, ResultSetConsumer consumer)
throws RDFServiceException {
Dataset d = getDataset();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
consumer.processResultSet(qe.execSelect());
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
consumer.processResultSet(qe.execSelect());
} finally {
qe.close();
}
} finally {
qe.close();
d.close();
}
}

@Override
public boolean sparqlAskQuery(String query) throws RDFServiceException {
Dataset d = getDataset();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
return qe.execAsk();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
return qe.execAsk();
} finally {
qe.close();
}
} finally {
qe.close();
d.close();
}
}

Expand All @@ -507,15 +527,16 @@ public List<String> getGraphURIs() throws RDFServiceException {
if (rebuildGraphURICache) {
synchronized (RDFServiceJena.class) {
if (rebuildGraphURICache) {
Dataset d = getDataset();
try {
Dataset d = getDataset();
Iterator<String> nameIt = d.listNames();
graphURIs.clear();
while (nameIt.hasNext()) {
graphURIs.add(nameIt.next());
}
return graphURIs;
} finally {
d.close();
rebuildGraphURICache = false;
}
}
Expand Down Expand Up @@ -546,19 +567,23 @@ public void serializeGraph(String graphURI, OutputStream outputStream)

private void serialize(OutputStream outputStream, String query) throws RDFServiceException {
Dataset d = getDataset();
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
ResultSet resultSet = qe.execSelect();
if (resultSet.getResultVars().contains("g")) {
Iterator<Quad> quads = new ResultSetQuadsIterator(resultSet);
RDFDataMgr.writeQuads(outputStream, quads);
} else {
Iterator<Triple> triples = new ResultSetTriplesIterator(resultSet);
RDFDataMgr.writeTriples(outputStream, triples);
Query q = createQuery(query);
QueryExecution qe = createQueryExecution(query, q, d);
try {
ResultSet resultSet = qe.execSelect();
if (resultSet.getResultVars().contains("g")) {
Iterator<Quad> quads = new ResultSetQuadsIterator(resultSet);
RDFDataMgr.writeQuads(outputStream, quads);
} else {
Iterator<Triple> triples = new ResultSetTriplesIterator(resultSet);
RDFDataMgr.writeTriples(outputStream, triples);
}
} finally {
qe.close();
}
} finally {
qe.close();
d.close();
}
}

Expand Down Expand Up @@ -612,6 +637,8 @@ public long countTriples(RDFNode subject, RDFNode predicate, RDFNode object) thr
Literal literal = soln.getLiteral("count");
return literal.getLong();
}
} finally {
d.close();
}

return 0;
Expand All @@ -637,8 +664,12 @@ public Model getTriples(RDFNode subject, RDFNode predicate, RDFNode object, long
Model triples = ModelFactory.createDefaultModel();

Dataset d = getDataset();
try (QueryExecution qexec = QueryExecutionFactory.create(query, d, map)) {
qexec.execConstruct(triples);
try {
try (QueryExecution qexec = QueryExecutionFactory.create(query, d, map)) {
qexec.execConstruct(triples);
}
} finally {
d.close();
}

return triples;
Expand Down

0 comments on commit 7215809

Please sign in to comment.