Search in sources :

Example 1 with BeginTx

use of org.neo4j.ha.BeginTx in project neo4j by neo4j.

the class IndexOperationsIT method put_if_absent_works_across_instances.

@Test
public void put_if_absent_works_across_instances() throws Exception {
    // GIVEN
    // -- two instances, each begin a transaction
    String key = "key2", value = "value2";
    HighlyAvailableGraphDatabase db1 = cluster.getMaster(), db2 = cluster.getAnySlave();
    long node = createNode(db1, key, value, false);
    cluster.sync();
    OtherThreadExecutor<HighlyAvailableGraphDatabase> w1 = new OtherThreadExecutor<>("w1", db1);
    OtherThreadExecutor<HighlyAvailableGraphDatabase> w2 = new OtherThreadExecutor<>("w2", db2);
    Transaction tx1 = w1.execute(new BeginTx());
    Transaction tx2 = w2.execute(new BeginTx());
    // WHEN
    // -- second instance does putIfAbsent --> null
    assertNull(w2.execute(new PutIfAbsent(node, key, value)));
    // -- get a future to first instance putIfAbsent. Wait for it to go and await the lock
    Future<Node> w1Future = w1.executeDontWait(new PutIfAbsent(node, key, value));
    w1.waitUntilWaiting();
    // -- second instance completes tx
    w2.execute(new FinishTx(tx2, true));
    tx2.success();
    tx2.close();
    // THEN
    // -- first instance can complete the future with a non-null result
    assertNotNull(w1Future.get());
    w1.execute(new FinishTx(tx1, true));
    // -- assert the index has got one entry and both instances have the same data
    assertNodeAndIndexingExists(db1, node, key, value);
    assertNodeAndIndexingExists(db2, node, key, value);
    cluster.sync();
    assertNodeAndIndexingExists(cluster.getAnySlave(db1, db2), node, key, value);
    w2.close();
    w1.close();
}
Also used : OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) Transaction(org.neo4j.graphdb.Transaction) FinishTx(org.neo4j.ha.FinishTx) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) Node(org.neo4j.graphdb.Node) BeginTx(org.neo4j.ha.BeginTx) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Node (org.neo4j.graphdb.Node)1 Transaction (org.neo4j.graphdb.Transaction)1 BeginTx (org.neo4j.ha.BeginTx)1 FinishTx (org.neo4j.ha.FinishTx)1 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)1 OtherThreadExecutor (org.neo4j.test.OtherThreadExecutor)1