This issue does seem to happen consistently. It isn't immediate, but after our application has been for some time, that is when it will usually happen. I don't have an exact timeframe for how long it will take to fail though. Here are some code snippets.
Setting up the connection:
private RDFDataSourceService createSemanticRepo(final IRDFDataSourceInfo sourceInfo) {
final String baseUrl = sourceInfo.getUrl();
final String id = sourceInfo.getRepositoryId();
final String username = sourceInfo.getUsername();
// Get the password from secure storage
String password = "";
if (sourceInfo instanceof IEncryptedDataValue) {
byte[] pwd = ((IEncryptedDataValue) sourceInfo).getSecurePassword();
if (pwd != null && pwd.length > 0) {
password = new String(pwd);
}
} else {
password = new String(sourceInfo.getPassword());
}
final Repository repo;
if (sourceInfo.getConnectionType() == RDFConnectionType.STARDOG) {
String queryEndpoint = baseUrl + "/" + id;
repo = new StardogRepository(ConnectionConfiguration.from(queryEndpoint).reasoning(sourceInfo.isReasoner()).credentials(username, password));
} else {
repo = new HTTPRepository(baseUrl, id);
((HTTPRepository) repo).setHttpClientSessionManager(new CustomHttpClientSessionManager());
if (username != null && password != null) {
((HTTPRepository) repo).setUsernameAndPassword(username, password);
}
}
repo.init();
try (RepositoryConnection conn = repo.getConnection()) {
ImportLoader loader = new ImportLoader(this.importResolver, conn.getParserConfig(), conn.getValueFactory());
SemanticRepository semanticRepository = new SemanticRepository(id, repo, this.settings, loader);
return new RDFDataSourceService(semanticRepository, sourceInfo.getId(), sourceInfo.isReasoner(), this.job);
}
}
SemanticQuery class:
private T internalExecute(final IPipelineContextLogger contextLogger) throws MemoryException {
T result = createEmptyResult();
long startTime = System.currentTimeMillis();
String query = "";
try (RepositoryConnection conn = this.repo.getConnection()) {
for (int i = 0; i < this.queries.length; i++) {
query = this.queries[i];
O op = createOperation(query, conn);
if (this.dataset != null) {
op.setDataset(this.dataset);
}
op.setIncludeInferred(this.reasoning);
for (Entry<String, Value> entry : this.bindings.entrySet()) {
op.setBinding(entry.getKey(), entry.getValue());
}
LOG.trace("Query to {}:\n {}", this.repo.toString(), op.toString());
result = executeOperation(op, result);
}
return result;
} catch (RDF4JException e) {
e.printStackTrace();
throw new ChampionException("Failed to execute query " + query, e);
}
}
Semantic Tuple Query:
class SemanticTupleQuery extends SemanticQuery<TupleQuery, List<Value[]>> {
public SemanticTupleQuery(final String[] queries, final Repository repo, final Dataset dataset, final ImportLoader loader) {
super(queries, repo, dataset, loader);
}
@Override
protected TupleQuery createOperation(final String query, final RepositoryConnection conn) throws RDF4JException {
return conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
}
@Override
protected List<Value[]> executeOperation(final TupleQuery tupleQuery, final List<Value[]> tuples) throws RDF4JException {
try (TupleQueryResult result = tupleQuery.evaluate()) {
List<String> vars = result.getBindingNames();
int n = vars.size();
while (result.hasNext()) {
Value[] tuple = new Value[n];
BindingSet binding = result.next();
for (int i = 0; i < n; i++) {
String var = vars.get(i);
tuple[i] = binding.getValue(var);
}
tuples.add(tuple);
}
}
return tuples;
}
@Override
protected List<Value[]> createEmptyResult() {
return new ArrayList<>();
}
}
Semantic Update Query:
class SemanticUpdateQuery extends SemanticQuery<Update, Void> {
public SemanticUpdateQuery(final String[] queries, final Repository repo, final Dataset dataset, final ImportLoader loader) {
super(queries, repo, dataset, loader);
}
@Override
protected Update createOperation(final String query, final RepositoryConnection conn) throws RDF4JException {
return conn.prepareUpdate(QueryLanguage.SPARQL, query);
}
@Override
protected Void executeOperation(final Update updateQuery, final Void result) throws RDF4JException {
updateQuery.execute();
return result;
}
@Override
protected Void createEmptyResult() {
return null;
}
}
I hope this helps. Please let me know if you need anything else.