use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.
the class TransactionHookIT method shouldRollbackOnFailureInBeforeCommit.
@Test
public void shouldRollbackOnFailureInBeforeCommit() throws Exception {
// Given
TransactionHook hook = mock(TransactionHook.class);
final String message = "Original";
when(hook.beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class))).thenReturn(new TransactionHook.Outcome() {
@Override
public boolean isSuccessful() {
return false;
}
@Override
public Throwable failure() {
return new Throwable(message);
}
});
kernel.registerTransactionHook(hook);
// When
DataWriteOperations ops = dataWriteOperationsInNewTransaction();
ops.nodeCreate();
try {
commit();
fail("Expected this to fail.");
} catch (org.neo4j.kernel.api.exceptions.TransactionFailureException e) {
assertThat(e.getCause().getMessage(), equalTo("Transaction handler failed."));
assertThat(e.getCause().getCause().getMessage(), equalTo(message));
}
// Then
verify(hook).beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class));
verify(hook).afterRollback(any(ReadableTransactionState.class), any(KernelTransaction.class), any(TransactionHook.Outcome.class));
verifyNoMoreInteractions(hook);
}
use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.
the class LabelIT method addingAndRemovingLabelInSameTxShouldHaveNoEffect.
@Test
public void addingAndRemovingLabelInSameTxShouldHaveNoEffect() throws Exception {
// Given a node with a label
Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
int label = statement.tokenWriteOperations().labelGetOrCreateForName("Label 1");
long node = statement.dataWriteOperations().nodeCreate();
statement.dataWriteOperations().nodeAddLabel(node, label);
commit();
// When I add and remove that label in the same tx
DataWriteOperations dataWriteOperations = dataWriteOperationsInNewTransaction();
dataWriteOperations.nodeRemoveLabel(node, label);
dataWriteOperations.nodeAddLabel(node, label);
// Then commit should not throw exceptions
commit();
// And then the node should have the label
assertTrue(readOperationsInNewTransaction().nodeHasLabel(node, label));
}
use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.
the class NodeGetUniqueFromIndexSeekIT method shouldBlockUniqueIndexSeekFromCompetingTransaction.
@Test(timeout = 1000)
public void shouldBlockUniqueIndexSeekFromCompetingTransaction() throws Exception {
// This is the interleaving that we are trying to verify works correctly:
// ----------------------------------------------------------------------
// Thread1 (main) : Thread2
// create unique node :
// lookup(node) :
// open start latch ----->
// | : lookup(node)
// wait for T2 to block : |
// : *block*
// commit ---------------> *unblock*
// wait for T2 end latch : |
// : finish transaction
// : open end latch
// *unblock* <-------------‘
// assert that we complete before timeout
final DoubleLatch latch = new DoubleLatch();
final NewIndexDescriptor index = createUniquenessConstraint(labelId, propertyId1);
final String value = "value";
DataWriteOperations dataStatement = dataWriteOperationsInNewTransaction();
long nodeId = dataStatement.nodeCreate();
dataStatement.nodeAddLabel(nodeId, labelId);
// This adds the node to the unique index and should take an index write lock
dataStatement.nodeSetProperty(nodeId, Property.stringProperty(propertyId1, value));
Runnable runnableForThread2 = () -> {
latch.waitForAllToStart();
try (Transaction tx = db.beginTx()) {
try (Statement statement1 = statementContextSupplier.get()) {
statement1.readOperations().nodeGetFromUniqueIndexSeek(index, exact(propertyId1, value));
}
tx.success();
} catch (IndexNotFoundKernelException | IndexNotApplicableKernelException | IndexBrokenKernelException e) {
throw new RuntimeException(e);
} finally {
latch.finish();
}
};
Thread thread2 = new Thread(runnableForThread2, "Transaction Thread 2");
thread2.start();
latch.startAndWaitForAllToStart();
//noinspection UnusedLabel
spinUntilBlocking: for (; ; ) {
if (thread2.getState() == Thread.State.TIMED_WAITING || thread2.getState() == Thread.State.WAITING) {
break;
}
Thread.yield();
}
commit();
latch.waitForAllToFinish();
}
use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.
the class NodeGetUniqueFromIndexSeekIT method createNodeWithValue.
private long createNodeWithValue(String value) throws KernelException {
DataWriteOperations dataStatement = dataWriteOperationsInNewTransaction();
long nodeId = dataStatement.nodeCreate();
dataStatement.nodeAddLabel(nodeId, labelId);
dataStatement.nodeSetProperty(nodeId, Property.stringProperty(propertyId1, value));
commit();
return nodeId;
}
use of org.neo4j.kernel.api.DataWriteOperations in project neo4j by neo4j.
the class PropertyIT method shouldBeAbleToRemoveResetAndTwiceRemovePropertyOnRelationship.
@Test
public void shouldBeAbleToRemoveResetAndTwiceRemovePropertyOnRelationship() throws Exception {
// given
Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
int prop = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("foo");
int type = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName("RELATED");
long startNodeId = statement.dataWriteOperations().nodeCreate();
long endNodeId = statement.dataWriteOperations().nodeCreate();
long rel = statement.dataWriteOperations().relationshipCreate(type, startNodeId, endNodeId);
statement.dataWriteOperations().relationshipSetProperty(rel, property(prop, "bar"));
commit();
// when
DataWriteOperations dataWriteOperations = dataWriteOperationsInNewTransaction();
dataWriteOperations.relationshipRemoveProperty(rel, prop);
dataWriteOperations.relationshipSetProperty(rel, property(prop, "bar"));
dataWriteOperations.relationshipRemoveProperty(rel, prop);
dataWriteOperations.relationshipRemoveProperty(rel, prop);
commit();
// then
ReadOperations readOperations = readOperationsInNewTransaction();
assertThat(readOperations.relationshipGetProperty(rel, prop), nullValue());
}
Aggregations