@Test public void testClose() { try { con.close(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); fail("Operation on connection after close should result in IllegalStateException"); } catch (IllegalStateException e) { // do nothing, this is expected } catch (SailException e) { fail(e.getMessage()); } }
@After public void tearDown() throws Exception { try { if (con.isOpen()) { con.rollback(); con.close(); } } finally { sail.shutDown(); sail = null; } }
@Test public void testDualConnections() throws Exception { SailConnection con2 = sail.getConnection(); try { assertEquals(0, countAllElements()); con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter, context1); con.addStatement(guernica, RDF.TYPE, painting, context1); con.commit(); assertEquals(4, countAllElements()); con2.begin(); con2.addStatement(RDF.NIL, RDF.TYPE, RDF.LIST); String query = "SELECT S, P, O FROM {S} P {O}"; ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, query, null); assertEquals( 5, countElements( con2.evaluate( tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false))); Runnable clearer = new Runnable() { public void run() { try { con.begin(); con.clear(); con.commit(); } catch (SailException e) { throw new RuntimeException(e); } } }; Thread thread = new Thread(clearer); thread.start(); Thread.yield(); Thread.yield(); con2.commit(); thread.join(); } finally { con2.close(); } }
/** Not compatible with our MVCC semantics. */ private void __testMultiThreadedAccess() { Runnable runnable = new Runnable() { SailConnection sharedCon = con; public void run() { assertTrue(sharedCon != null); try { while (sharedCon.isActive()) { Thread.sleep(10); } sharedCon.begin(); sharedCon.addStatement(painter, RDF.TYPE, RDFS.CLASS); sharedCon.commit(); // wait a bit to allow other thread to add stuff as well. Thread.sleep(500L); CloseableIteration<? extends Statement, SailException> result = sharedCon.getStatements(null, null, null, true); assertTrue(result.hasNext()); int numberOfStatements = 0; while (result.hasNext()) { numberOfStatements++; Statement st = result.next(); assertTrue(st.getSubject().equals(painter) || st.getSubject().equals(picasso)); assertTrue(st.getPredicate().equals(RDF.TYPE)); assertTrue(st.getObject().equals(RDFS.CLASS) || st.getObject().equals(painter)); } assertTrue( "we should have retrieved statements from both threads", numberOfStatements == 2); } catch (SailException e) { e.printStackTrace(); fail(e.getMessage()); } catch (InterruptedException e) { fail(e.getMessage()); } // let this thread sleep so the other thread can invoke close() // first. try { Thread.sleep(1000L); // the connection should now be closed (by the other thread), // invoking any further operation should cause a // IllegalStateException sharedCon.getStatements(null, null, null, true); fail("should have caused an IllegalStateException"); } catch (InterruptedException e) { fail(e.getMessage()); } catch (SailException e) { e.printStackTrace(); fail(e.getMessage()); } catch (IllegalStateException e) { // do nothing, this is the expected behaviour } } }; // end anonymous class declaration // execute the other thread Thread newThread = new Thread(runnable, "B (parallel)"); newThread.start(); try { while (con.isActive()) { Thread.sleep(10); } con.begin(); con.addStatement(picasso, RDF.TYPE, painter); con.commit(); // let this thread sleep to enable other thread to finish its business. Thread.sleep(1000L); con.close(); } catch (SailException e) { e.printStackTrace(); fail(e.getMessage()); } catch (InterruptedException e) { fail(e.getMessage()); } }