Skip to content

Commit

Permalink
fixing repository connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ddtxra committed May 27, 2015
1 parent 48c2cc4 commit 6ceb18b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
3 changes: 2 additions & 1 deletion default/queries/q0203.rq
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Binary file modified sparql-playground.war
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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();

Expand All @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -274,4 +256,9 @@ public boolean isDataLoadAllowed() {
return rep.getDataDir() == null;
}

@Override
public void afterPropertiesSet() throws Exception {
conn = rep.getConnection();
}

}

0 comments on commit 6ceb18b

Please sign in to comment.