Search in sources :

Example 1 with Transaction

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();
    }
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) Transaction(org.neo4j.graphdb.Transaction) Random(java.util.Random) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 2 with Transaction

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;
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) ArrayList(java.util.ArrayList)

Example 3 with Transaction

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();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction)

Example 4 with Transaction

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();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CountingAdversary(org.neo4j.adversaries.CountingAdversary) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Transaction(org.neo4j.graphdb.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with Transaction

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();
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) LabelTokenHolder(org.neo4j.kernel.impl.core.LabelTokenHolder) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Aggregations

Transaction (org.neo4j.graphdb.Transaction)2409 Node (org.neo4j.graphdb.Node)1086 Test (org.junit.jupiter.api.Test)751 Test (org.junit.Test)607 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)352 Relationship (org.neo4j.graphdb.Relationship)307 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)302 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)241 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)177 Label (org.neo4j.graphdb.Label)154 Result (org.neo4j.graphdb.Result)142 HashMap (java.util.HashMap)105 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)104 MethodSource (org.junit.jupiter.params.provider.MethodSource)103 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)86 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)77 File (java.io.File)74 ArrayList (java.util.ArrayList)73 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)67 Path (java.nio.file.Path)64