diff --git a/default/queries/q0203.rq b/default/queries/q0203.rq index 1938993d..e2461984 100755 --- a/default/queries/q0203.rq +++ b/default/queries/q0203.rq @@ -9,10 +9,11 @@ select ?thing where { ?thing tto:sex "female" . } - # Use the same name of the variable in the 2 statements # It is the name of the variable that enforces the constraint +# Note the dot "." which must be added in the first statement, otherwise we get a MalformedQueryException + # Hint: Use the semicolon ';' to refer to the previous subject # # select ?thing where { diff --git a/sparql-playground.war b/sparql-playground.war index d2c1900d..95b76130 100644 Binary files a/sparql-playground.war and b/sparql-playground.war differ diff --git a/src/main/java/ch/isb/sib/sparql/tutorial/repository/SesameRepository.java b/src/main/java/ch/isb/sib/sparql/tutorial/repository/SesameRepository.java index 76a2cbb8..05f35749 100644 --- a/src/main/java/ch/isb/sib/sparql/tutorial/repository/SesameRepository.java +++ b/src/main/java/ch/isb/sib/sparql/tutorial/repository/SesameRepository.java @@ -37,6 +37,7 @@ import org.openrdf.rio.Rio; import org.openrdf.sail.memory.MemoryStore; import org.openrdf.sail.nativerdf.NativeStore; +import org.springframework.beans.factory.InitializingBean; import ch.isb.sib.sparql.tutorial.Application; import ch.isb.sib.sparql.tutorial.exception.SparqlTutorialException; @@ -48,7 +49,7 @@ * */ @org.springframework.stereotype.Repository -public class SesameRepository { +public class SesameRepository implements InitializingBean{ private static final Log logger = LogFactory.getLog(SesameRepository.class); @@ -58,13 +59,12 @@ public class SesameRepository { private Repository rep = null; private SailRepository testRepo; + private RepositoryConnection conn = null; public TupleQueryResult query(String sparqlQuery) { - RepositoryConnection conn = null; try { - conn = rep.getConnection(); TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQuery); return q.evaluate(); @@ -77,23 +77,12 @@ public TupleQueryResult query(String sparqlQuery) { } catch (QueryEvaluationException e) { e.printStackTrace(); throw new SparqlTutorialException(e); - } finally { - if(conn != null){ - try { - conn.close(); - } catch (RepositoryException e) { - e.printStackTrace(); - throw new SparqlTutorialException(e); - } - } - } + } } public void query(String queryString, TupleQueryResultHandler handler) { - RepositoryConnection conn = null; try { - conn = rep.getConnection(); TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); q.evaluate(handler); @@ -109,16 +98,7 @@ public void query(String queryString, TupleQueryResultHandler handler) { } catch (QueryEvaluationException e) { e.printStackTrace(); throw new SparqlTutorialException(e); - } finally { - if(conn != null){ - try { - conn.close(); - } catch (RepositoryException e) { - e.printStackTrace(); - throw new SparqlTutorialException(e); - } - } - } + } } @PostConstruct @@ -260,7 +240,9 @@ public void loadData(String data) { public long countTriplets() { try { TupleQueryResult result = this.query("SELECT (COUNT(*) AS ?no) { ?s ?p ?o }"); - return Long.valueOf(result.next().getBinding("no").getValue().stringValue()); + long n = Long.valueOf(result.next().getBinding("no").getValue().stringValue()); + result.close(); + return n; } catch (NumberFormatException e) { e.printStackTrace(); throw new SparqlTutorialException(e); @@ -274,4 +256,9 @@ public boolean isDataLoadAllowed() { return rep.getDataDir() == null; } + @Override + public void afterPropertiesSet() throws Exception { + conn = rep.getConnection(); + } + }