Java Code Examples for org.eclipse.rdf4j.repository.RepositoryConnection#rollback()

The following examples show how to use org.eclipse.rdf4j.repository.RepositoryConnection#rollback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSHACLTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void upload(Repository rep, Model dataGraph) {
	RepositoryConnection con = rep.getConnection();

	try {
		con.begin();
		con.add(dataGraph);
		con.commit();
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
	}
}
 
Example 2
Source File: AbstractLuceneSailSpinTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testDistanceFunction() throws Exception {
	RepositoryConnection connection = getConnection();
	String queryStr = "prefix geo:  <" + GEO.NAMESPACE + ">" + "prefix geof: <" + GEOF.NAMESPACE + ">"
			+ "prefix search: <" + LuceneSailSchema.NAMESPACE + ">"
			+ "select ?toUri ?fromUri ?dist where {(?from ?range ?units geo:asWKT search:distance)"
			+ "search:withinDistance (?toUri ?to ?dist) ."
			+ "?toUri a <urn:geo/Landmark>. ?fromUri geo:asWKT ?from; <urn:geo/maxDistance> ?range.}";
	try {
		TupleQuery query = connection.prepareTupleQuery(QueryLanguage.SPARQL, queryStr);
		query.setBinding("units", GEOF.UOM_METRE);

		printTupleResult(query);
		try (TupleQueryResult result = query.evaluate()) {
			int count = countTupleResults(result);
			Assert.assertEquals(2, count);
		}
	} catch (Exception e) {
		connection.rollback();
		throw e;
	} finally {
		connection.commit();
	}
}
 
Example 3
Source File: Repositories.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Opens a {@link RepositoryConnection} to the given Repository within a transaction, sends the connection to the
 * given {@link Function}, before either rolling back the transaction if it failed, or committing the transaction if
 * it was successful.
 *
 * @param                 <T> The type of the return value.
 * @param repository      The {@link Repository} to open a connection to.
 * @param processFunction A {@link Function} that performs an action on the connection and returns a result.
 * @return The result of applying the function.
 * @throws RepositoryException              If there was an exception dealing with the Repository.
 * @throws UnknownTransactionStateException If the transaction state was not properly recognised. (Optional specific
 *                                          exception)
 */
public static <T> T get(Repository repository, Function<RepositoryConnection, T> processFunction)
		throws RepositoryException, UnknownTransactionStateException {
	RepositoryConnection conn = null;

	try {
		conn = repository.getConnection();
		conn.begin();
		T result = processFunction.apply(conn);
		conn.commit();
		return result;
	} catch (RepositoryException e) {
		if (conn != null && conn.isActive()) {
			conn.rollback();
		}
		throw e;
	} finally {
		if (conn != null && conn.isOpen()) {
			conn.close();
		}
	}
}
 
Example 4
Source File: SPARQLComplianceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void upload(IRI graphURI, Resource context) throws Exception {
	RepositoryConnection con = getDataRepository().getConnection();

	try {
		con.begin();
		RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString()).orElse(RDFFormat.TURTLE);
		RDFParser rdfParser = Rio.createParser(rdfFormat, getDataRepository().getValueFactory());
		rdfParser.setVerifyData(false);
		rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
		// rdfParser.setPreserveBNodeIDs(true);

		RDFInserter rdfInserter = new RDFInserter(con);
		rdfInserter.enforceContext(context);
		rdfParser.setRDFHandler(rdfInserter);

		URL graphURL = new URL(graphURI.toString());
		InputStream in = graphURL.openStream();
		try {
			rdfParser.parse(in, graphURI.toString());
		} finally {
			in.close();
		}

		con.commit();
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
	}
}
 
Example 5
Source File: SPARQLQueryTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void upload(IRI graphURI, Resource context) throws Exception {
	RepositoryConnection con = dataRep.getConnection();

	try {
		con.begin();
		RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString()).orElse(RDFFormat.TURTLE);
		RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory());
		rdfParser.setVerifyData(false);
		rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
		// rdfParser.setPreserveBNodeIDs(true);

		RDFInserter rdfInserter = new RDFInserter(con);
		rdfInserter.enforceContext(context);
		rdfParser.setRDFHandler(rdfInserter);

		URL graphURL = new URL(graphURI.toString());
		InputStream in = graphURL.openStream();
		try {
			rdfParser.parse(in, graphURI.toString());
		} finally {
			in.close();
		}

		con.commit();
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
	}
}
 
Example 6
Source File: SPARQLUpdateConformanceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void runTest() throws Exception {
	RepositoryConnection con = dataRep.getConnection();
	RepositoryConnection erCon = expectedResultRepo.getConnection();
	try {
		String updateString = readUpdateString();

		con.begin();

		Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFileURL);
		update.setDataset(dataset);
		update.execute();

		con.commit();

		// check default graph
		logger.info("checking default graph");
		compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource) null)),
				Iterations.asList(erCon.getStatements(null, null, null, true, (Resource) null)));

		for (String namedGraph : inputNamedGraphs.keySet()) {
			logger.info("checking named graph {}", namedGraph);
			IRI contextURI = con.getValueFactory().createIRI(namedGraph.replaceAll("\"", ""));
			compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
					Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)));
		}
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
		erCon.close();
	}
}
 
Example 7
Source File: SPARQL11UpdateComplianceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void runTest() throws Exception {

	logger.debug("running {}", getName());

	RepositoryConnection con = dataRep.getConnection();
	RepositoryConnection erCon = expectedResultRepo.getConnection();
	try {
		String updateString = readUpdateString();

		con.begin();

		Update update = con.prepareUpdate(QueryLanguage.SPARQL, updateString, requestFile);
		update.setDataset(dataset);
		update.execute();

		con.commit();

		// check default graph
		logger.info("checking default graph");
		compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, (Resource) null)),
				Iterations.asList(erCon.getStatements(null, null, null, true, (Resource) null)));

		for (String namedGraph : inputNamedGraphs.keySet()) {
			logger.info("checking named graph {}", namedGraph);
			IRI contextURI = con.getValueFactory().createIRI(namedGraph.replaceAll("\"", ""));
			compareGraphs(Iterations.asList(con.getStatements(null, null, null, true, contextURI)),
					Iterations.asList(erCon.getStatements(null, null, null, true, contextURI)));
		}
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
		erCon.close();
	}
}
 
Example 8
Source File: IsolationLevelTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected boolean isSupported(IsolationLevels level) throws RepositoryException {
	RepositoryConnection con = store.getConnection();
	try {
		con.begin(level);
		return true;
	} catch (UnknownTransactionStateException e) {
		return false;
	} finally {
		try {
			con.rollback();
		} finally {
			con.close();
		}
	}
}