use of org.neo4j.graphdb.Transaction in project neo4j-clean-remote-db-addon by jexp.
the class DeleteDatabaseTest method createData.
private void createData(GraphDatabaseAPI db, int max) {
Transaction tx = db.beginTx();
try {
final IndexManager indexManager = db.index();
Node[] nodes = new Node[max];
for (int i = 0; i < max; i++) {
nodes[i] = db.createNode();
final Index<Node> index = indexManager.forNodes("node_index_" + String.valueOf(i % 5));
index.add(nodes[i], "ID", i);
}
Random random = new Random();
for (int i = 0; i < max * 2; i++) {
int from = random.nextInt(max);
final int to = (from + 1 + random.nextInt(max - 1)) % max;
final Relationship relationship = nodes[from].createRelationshipTo(nodes[to], DynamicRelationshipType.withName("TEST_" + i));
final Index<Relationship> index = indexManager.forRelationships("rel_index_" + String.valueOf(i % 5));
index.add(relationship, "ID", i);
}
tx.success();
} finally {
tx.finish();
}
}
use of org.neo4j.graphdb.Transaction in project javaee7-samples by javaee-samples.
the class PersonSessionBean method getPersons.
public List<BackingBean> getPersons() {
List<BackingBean> beans = new ArrayList();
try (Transaction tx = graphDb.beginTx()) {
for (String key : firstNode.getPropertyKeys()) {
BackingBean bean = new BackingBean();
Person p = Person.fromString((String) firstNode.getProperty(key));
bean.setName(p.getName());
bean.setAge(p.getAge());
for (Relationship r : firstNode.getRelationships(RelTypes.SPOUSE, RelTypes.SISTER, RelTypes.BROTHER)) {
if (r.isType(RelTypes.SPOUSE)) {
bean.setRelationship("spouse");
break;
} else if (r.isType(RelTypes.SISTER)) {
bean.setRelationship("sister");
break;
} else if (r.isType(RelTypes.BROTHER)) {
bean.setRelationship("brother");
break;
}
}
beans.add(bean);
}
tx.success();
}
return beans;
}
use of org.neo4j.graphdb.Transaction in project javaee7-samples by javaee-samples.
the class PersonSessionBean method createPerson.
public void createPerson() {
try (Transaction tx = graphDb.beginTx()) {
firstNode.setProperty(backingBean.getName(), backingBean.person1String());
secondNode.setProperty(backingBean.getName2(), backingBean.person2String());
switch(backingBean.getRelationship()) {
case "spouse":
firstNode.createRelationshipTo(secondNode, RelTypes.SPOUSE);
break;
case "brother":
firstNode.createRelationshipTo(secondNode, RelTypes.BROTHER);
break;
case "sister":
firstNode.createRelationshipTo(secondNode, RelTypes.SISTER);
break;
}
tx.success();
}
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class CountsRotationTest method possibleToShutdownDbWhenItIsNotHealthyAndNotAllTransactionsAreApplied.
@Test(timeout = 60_000)
public void possibleToShutdownDbWhenItIsNotHealthyAndNotAllTransactionsAreApplied() throws Exception {
// adversary that makes page cache throw exception when node store is used
ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, true), NodeStore.class);
adversary.disable();
GraphDatabaseService db = AdversarialPageCacheGraphDatabaseFactory.create(fs, adversary).newEmbeddedDatabaseBuilder(dir).newGraphDatabase();
CountDownLatch txStartLatch = new CountDownLatch(1);
CountDownLatch txCommitLatch = new CountDownLatch(1);
Future<?> result = ForkJoinPool.commonPool().submit(() -> {
try (Transaction tx = db.beginTx()) {
txStartLatch.countDown();
db.createNode();
await(txCommitLatch);
tx.success();
}
});
await(txStartLatch);
adversary.enable();
txCommitLatch.countDown();
try {
result.get();
fail("Exception expected");
} catch (ExecutionException ee) {
// transaction is expected to fail because write through the page cache fails
assertThat(ee.getCause(), instanceOf(TransactionFailureException.class));
}
adversary.disable();
// shutdown should complete without any problems
db.shutdown();
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class CountsRotationTest method shouldRotateCountsStoreWhenRotatingLog.
@Test
public void shouldRotateCountsStoreWhenRotatingLog() throws IOException {
// GIVEN
GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
// WHEN doing a transaction (actually two, the label-mini-tx also counts)
try (Transaction tx = db.beginTx()) {
db.createNode(B);
tx.success();
}
// and rotating the log (which implies flushing)
checkPoint(db);
// and creating another node after it
try (Transaction tx = db.beginTx()) {
db.createNode(C);
tx.success();
}
// THEN
assertTrue(fs.fileExists(alphaStoreFile()));
assertTrue(fs.fileExists(betaStoreFile()));
final PageCache pageCache = db.getDependencyResolver().resolveDependency(PageCache.class);
try (Lifespan life = new Lifespan()) {
CountsTracker store = life.add(createCountsTracker(pageCache));
// NOTE since the rotation happens before the second transaction is committed we do not see those changes
// in the stats
// a transaction for creating the label and a transaction for the node
assertEquals(BASE_TX_ID + 1 + 1, store.txId());
assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
// one for all nodes and one for the created "B" label
assertEquals(1 + 1, store.totalEntriesStored());
assertEquals(1 + 1, allRecords(store).size());
}
// on the other hand the tracker should read the correct value by merging data on disk and data in memory
final CountsTracker tracker = db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getCounts();
assertEquals(1 + 1, tracker.nodeCount(-1, newDoubleLongRegister()).readSecond());
final LabelTokenHolder holder = db.getDependencyResolver().resolveDependency(LabelTokenHolder.class);
int labelId = holder.getIdByName(C.name());
assertEquals(1, tracker.nodeCount(labelId, newDoubleLongRegister()).readSecond());
db.shutdown();
}
Aggregations