use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.
the class T_TxnDeadlockTest method test.
//@Test
public void test() {
final StoreConnection storeConnection = StoreConnection.make(Location.mem());
//ExecutorService executor = Executors.newCachedThreadPool() ; // Not seen blocking.
// 4 blocks maybe 1 in 4 times
// 8 blocks (quad core) 2 in 3 times.
ExecutorService executor = Executors.newFixedThreadPool(8);
final AtomicInteger nbQuadruplesAdded = new AtomicInteger();
final CountDownLatch doneSignal = new CountDownLatch(CONCURRENT_RANDOM_OPERATIONS);
for (int i = 0; i < CONCURRENT_RANDOM_OPERATIONS; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
if (numberGenerator.nextInt(2) == 0) {
DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.WRITE);
txnGraph.add(new Quad(NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt())));
txnGraph.commit();
txnGraph.end();
nbQuadruplesAdded.incrementAndGet();
} else {
DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.READ);
txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
//Iterator<Quad> iter = txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
//Iter.count(iter) ; // Consume
txnGraph.end();
}
} finally {
doneSignal.countDown();
}
}
});
}
// shutdown is orderly so sync'ing up before the shutdown is nice but not needed.
try {
doneSignal.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
executor.shutdown();
}
DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.READ);
Iterator<Quad> result = txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
long count = 0;
while (result.hasNext()) {
result.next();
count++;
}
txnGraph.end();
StoreConnection.release(storeConnection.getLocation());
//System.out.println() ;
System.out.println("FINISHED");
// // This is unsafe - the quad adds may generate duplicates (ity's unlikly 4 random number reoccur but it's possible).
// Assert.assertEquals(count, nbQuadruplesAdded.get());
}
use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.
the class AbstractStoreConnections method store_6.
@Test
public void store_6() {
// Transaction - release - reattach
// This tests that the dataset is sync'ed when going into transactional mode.
StoreConnection sConn = getStoreConnection();
Location loc = sConn.getLocation();
DatasetGraphTxn dsgTxn = sConn.begin(ReadWrite.WRITE);
dsgTxn.add(q1);
assertTrue(dsgTxn.contains(q1));
dsgTxn.commit();
dsgTxn.end();
sConn.forceRecoverFromJournal();
assertTrue(sConn.getBaseDataset().contains(q1));
StoreConnection.release(loc);
sConn = StoreConnection.make(loc);
DatasetGraph dsg2 = sConn.getBaseDataset();
assertTrue(dsg2.contains(q1));
DatasetGraphTxn dsgTxn2 = sConn.begin(ReadWrite.READ);
assertTrue(dsgTxn2.contains(q1));
dsgTxn2.end();
}
use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.
the class AbstractStoreConnections method store_7.
@Test
public void store_7() {
// No transaction, plain update, then transaction.
// This tests that the dataset is sync'ed when going into transactional mode.
boolean nonTxnData = true;
StoreConnection sConn = getStoreConnection();
Location loc = sConn.getLocation();
DatasetGraph dsg = sConn.getBaseDataset();
if (nonTxnData) {
dsg.add(q);
TDB.sync(dsg);
assertTrue(dsg.contains(q));
}
DatasetGraphTxn dsgTxn = sConn.begin(ReadWrite.WRITE);
if (nonTxnData)
assertTrue(dsgTxn.contains(q));
dsgTxn.add(q1);
assertTrue(dsgTxn.contains(q1));
if (nonTxnData)
assertTrue(dsgTxn.contains(q));
dsgTxn.commit();
dsgTxn.end();
// Should have flushed to disk.
if (nonTxnData) {
sConn.forceRecoverFromJournal();
assertTrue(dsg.contains(q));
}
assertTrue(dsg.contains(q1));
// release via the transactional machinery
StoreConnection.release(loc);
sConn = null;
StoreConnection sConn2 = StoreConnection.make(loc);
DatasetGraph dsg2 = sConn2.getBaseDataset();
if (nonTxnData)
assertTrue(dsg2.contains(q));
assertTrue(dsg2.contains(q1));
DatasetGraphTxn dsgTxn2 = sConn2.begin(ReadWrite.READ);
if (nonTxnData)
assertTrue(dsgTxn2.contains(q));
assertTrue(dsgTxn2.contains(q1));
dsgTxn2.end();
// Check API methods work.
Dataset ds = TDBFactory.createDataset(loc);
ds.begin(ReadWrite.READ);
Model m = (q.isDefaultGraph() ? ds.getDefaultModel() : ds.getNamedModel("g"));
assertEquals(nonTxnData ? 2 : 1, m.size());
ds.end();
}
use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.
the class AbstractStoreConnections method store_3.
@Test(expected = TDBTransactionException.class)
public void store_3() {
// Expel.
StoreConnection sConn = getStoreConnection();
DatasetGraphTxn dsgR1 = sConn.begin(ReadWrite.WRITE);
StoreConnection.release(sConn.getLocation());
}
use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.
the class AbstractStoreConnections method store_1.
@Test
public void store_1() {
// Expel.
StoreConnection sConn = getStoreConnection();
DatasetGraphTxn dsgR1 = sConn.begin(ReadWrite.READ);
DatasetGraphTxn dsgW1 = sConn.begin(ReadWrite.WRITE);
dsgW1.add(q1);
dsgW1.commit();
dsgW1.end();
dsgR1.end();
assertTrue(sConn.isValid());
StoreConnection.release(sConn.getLocation());
assertFalse(sConn.isValid());
sConn = null;
StoreConnection sConn2 = getStoreConnection();
assertTrue(sConn2.isValid());
}
Aggregations