@Override @Deprecated public void setAutocommit(boolean autocommit) { assertModel(); if (autocommit == false) { try { this.connection.begin(); } catch (RepositoryException e) { throw new RuntimeException(e); } } else { try { this.connection.commit(); } catch(RepositoryException e) { throw new RuntimeException(e); } } }
@Test public void testAutoCommit() throws Exception { testCon.begin(); testCon.add(alice, name, nameAlice); assertTrue("Uncommitted update should be visible to own connection", testCon.hasStatement(alice, name, nameAlice, false)); testCon.commit(); assertTrue("Repository should contain statement after commit", testCon.hasStatement(alice, name, nameAlice, false)); }
@Test public void testRollback() throws Exception { testCon.begin(); testCon.add(alice, name, nameAlice); assertTrue("Uncommitted updates should be visible to own connection", testCon.hasStatement(alice, name, nameAlice, false)); testCon.rollback(); assertFalse("Repository should not contain statement after rollback", testCon.hasStatement(alice, name, nameAlice, false)); }
@Test public void testDuplicateFilter() throws Exception { testCon.begin(); testCon.add(bob, name, nameBob); testCon.add(bob, name, nameBob, context1); testCon.add(bob, name, nameBob, context2); testCon.commit(); RepositoryResult<Statement> result = testCon.getStatements(bob, name, null, true); result.enableDuplicateFilter(); int count = 0; while (result.hasNext()) { result.next(); count++; } assertThat(count, is(equalTo(1))); }
@Test public void testEmptyRollback() throws Exception { assertThat(testCon.isEmpty(), is(equalTo(true))); assertThat(testCon2.isEmpty(), is(equalTo(true))); testCon.begin(); testCon.add(vf.createBNode(), vf.createURI(URN_PRED), vf.createBNode()); assertThat(testCon.isEmpty(), is(equalTo(false))); assertThat(testCon2.isEmpty(), is(equalTo(true))); testCon.rollback(); assertThat(testCon.isEmpty(), is(equalTo(true))); assertThat(testCon2.isEmpty(), is(equalTo(true))); }
@Test @SuppressWarnings("unchecked") public void testNamespaces() throws RepositoryException { RepositoryConnection connection = repository.getConnection(); connection.begin(); connection.setNamespace("ns1","http://localhost/ns1/"); connection.setNamespace("ns2","http://localhost/ns2/"); connection.commit(); connection.begin(); connection.setNamespace("ns1","http://localhost/ns3/"); connection.commit(); connection.begin(); connection.removeNamespace("ns2"); connection.commit(); connection.begin(); Assert.assertEquals(1, Iterations.asList(connection.getNamespaces()).size());
RepositoryConnection conn = rep.getConnection(); try { conn.begin(); // start a transaction conn.add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon")); conn.add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon")); conn.add(f.createURI("http://example.org/", "Timon"),RDF.PREDICATE, f.createURI("http://example.org/", "eddy")); conn.add(f.createURI("http://example.org/", "Pumba"),RDF.PREDICATE, f.createURI("http://example.org/", "Timon")); conn.commit(); } finally { conn.close(); }
@GET @Path("/subject") @Produces(Namespaces.MIME_TYPE_JSON) public List<TriplePoJo> listSubjectUsage(@QueryParam("uri") String uri, @QueryParam("start") @DefaultValue("0") long offset, @QueryParam("limit") @DefaultValue(DEFAULT_BATCH_SIZE) int batchSize) { try { RepositoryConnection conn = sesameService.getConnection(); try { conn.begin(); URI r = getUriResource(conn, uri); if (r != null) return buildResultList(conn, r, null, null, null, offset, batchSize); else return Collections.emptyList(); } finally { conn.commit(); conn.close(); } } catch(RepositoryException ex) { handleRepositoryException(ex,InspectionWebService.class); return Collections.emptyList(); } }
@Test public void testTransactionIsolation() throws Exception { testCon.begin(); testCon.add(bob, name, nameBob); assertThat(testCon.hasStatement(bob, name, nameBob, false), is(equalTo(true))); assertThat(testCon2.hasStatement(bob, name, nameBob, false), is(equalTo(false))); testCon.commit(); testCon2.close(); testCon2 = testRepository.getConnection(); assertThat(testCon.hasStatement(bob, name, nameBob, false), is(equalTo(true))); assertThat(testCon2.hasStatement(bob, name, nameBob, false), is(equalTo(true))); }
@Test public void testSizeRollback() throws Exception { assertThat(testCon.size(), is(equalTo(0L))); assertThat(testCon2.size(), is(equalTo(0L))); testCon.begin(); testCon.add(vf.createBNode(), vf.createURI(URN_PRED), vf.createBNode()); assertThat(testCon.size(), is(equalTo(1L))); assertThat(testCon2.size(), is(equalTo(0L))); testCon.add(vf.createBNode(), vf.createURI(URN_PRED), vf.createBNode()); assertThat(testCon.size(), is(equalTo(2L))); assertThat(testCon2.size(), is(equalTo(0L))); testCon.rollback(); assertThat(testCon.size(), is(equalTo(0L))); assertThat(testCon2.size(), is(equalTo(0L))); }
@Test public void testDummyProvider() throws Exception { ClientResponse resp1 = ldclient.retrieveResource("http://localhost/resource1"); RepositoryConnection con1 = ModelCommons.asRepository(resp1.getData()).getConnection(); try { con1.begin(); Assert.assertEquals(3, con1.size()); con1.commit(); } finally { con1.close(); } ClientResponse resp2 = ldclient.retrieveResource("http://localhost/resource2"); RepositoryConnection con2 = ModelCommons.asRepository(resp2.getData()).getConnection(); try { con2.begin(); Assert.assertEquals(2, con2.size()); con2.commit(); } finally { con2.close(); } ClientResponse resp3 = ldclient.retrieveResource("http://localhost/resource3"); RepositoryConnection con3 = ModelCommons.asRepository(resp3.getData()).getConnection(); try { con3.begin(); Assert.assertEquals(2, con3.size()); con3.commit(); } finally { con3.close(); } }
/** * Handles a statement. * * The statements will be added up until chunk size is reached. * After a chunk of statements is added the transaction will be committed * and new transaction will be started. * @param stmnt * @throws RDFHandlerException */ @Override public void handleStatement(Statement stmnt) throws RDFHandlerException { try { // check if triple should be added to a specific graph if (dctx != null) { conn.add(stmnt, dctx); } else { conn.add(stmnt); } // check if chunk size is reached and transaction should be // committed count++; if (count >= size) { count = 0; conn.commit(); conn.begin(); } } catch (RepositoryException ex) { throw new RDFHandlerException(ex); } }
@GET @Path("/predicate") @Produces(Namespaces.MIME_TYPE_JSON) public List<TriplePoJo> listPredicatetUsage(@QueryParam("uri") String uri, @QueryParam("start") @DefaultValue("0") long offset, @QueryParam("limit") @DefaultValue(DEFAULT_BATCH_SIZE) int batchSize) { try { RepositoryConnection conn = sesameService.getConnection(); try { conn.begin(); URI r = getUriResource(conn, uri); if (r != null) return buildResultList(conn, null, r, null, null, offset, batchSize); else return Collections.emptyList(); } finally { conn.commit(); conn.close(); } } catch(RepositoryException ex) { handleRepositoryException(ex,InspectionWebService.class); return Collections.emptyList(); } }
@Test public void testAddRemove() throws Exception { URI FOAF_PERSON = vf.createURI("http://xmlns.com/foaf/0.1/Person"); final Statement stmt = vf.createStatement(bob, name, nameBob); testCon.add(bob, RDF.TYPE, FOAF_PERSON); testCon.begin(); testCon.add(stmt); testCon.remove(stmt); testCon.commit(); testCon.exportStatements(null, null, null, false, new RDFHandlerBase() { @Override public void handleStatement(Statement st) throws RDFHandlerException { assertThat(st, is(not(equalTo(stmt)))); } }); }
@Test public void testTransactionIsolationForRead() throws Exception { testCon.begin(); try { // Add but do not commit testCon.add(OWL.CLASS, RDFS.COMMENT, RDF.STATEMENT); assertTrue("Should be able to see uncommitted statement on same connection", testCon.hasStatement(OWL.CLASS, RDFS.COMMENT, RDF.STATEMENT, true)); assertFalse( "Should not be able to see uncommitted statement on separate connection outside transaction", testCon2.hasStatement(OWL.CLASS, RDFS.COMMENT, RDF.STATEMENT, true)); testCon2.begin(); try { assertFalse( "Should not be able to see uncommitted statement on separate connection inside transaction", testCon2.hasStatement(OWL.CLASS, RDFS.COMMENT, RDF.STATEMENT, true)); } finally { testCon2.rollback(); } } finally { testCon.rollback(); } }
public static void loadDataFromResources(final Repository repo, final String resource, final String baseURL) throws OpenRDFException, IOException { final RepositoryConnection cxn = repo.getConnection(); try { cxn.begin(); try { final InputStream is = SampleBlazegraphSesameEmbedded.class.getResourceAsStream(resource); if (is == null) { throw new IOException("Could not locate resource: " + resource); } final Reader reader = new InputStreamReader(new BufferedInputStream(is)); try { cxn.add(reader, baseURL, RDFFormat.N3); } finally { reader.close(); } cxn.commit(); } catch (OpenRDFException ex) { cxn.rollback(); throw ex; } } finally { // close the repository connection cxn.close(); } }
@GET @Path("/object") @Produces(Namespaces.MIME_TYPE_JSON) public List<TriplePoJo> listObjectUsage(@QueryParam("uri") String uri, @QueryParam("start") @DefaultValue("0") long offset, @QueryParam("limit") @DefaultValue(DEFAULT_BATCH_SIZE) int batchSize) { try { RepositoryConnection conn = sesameService.getConnection(); try { conn.begin(); URI r = getUriResource(conn, uri); if (r != null) return buildResultList(conn, null, null, r, null, offset, batchSize); else return Collections.emptyList(); } finally { conn.commit(); conn.close(); } } catch(RepositoryException ex) { handleRepositoryException(ex,InspectionWebService.class); return Collections.emptyList(); } }