use of org.neo4j.graphdb.event.TransactionEventListenerAdapter in project neo4j by neo4j.
the class TestTransactionEvents method modifiedPropertyCanByFurtherModifiedInBeforeCommit.
@Test
void modifiedPropertyCanByFurtherModifiedInBeforeCommit() {
// Given
// -- create node and set property on it in one transaction
final String key = "key";
final Object value1 = "the old value";
final Object value2 = "the new value";
final Node node;
try (Transaction tx = db.beginTx()) {
node = tx.createNode();
node.setProperty(key, "initial value");
tx.commit();
}
// -- register a tx listener which will override a property
TransactionEventListener<Void> listener = new TransactionEventListenerAdapter<>() {
@Override
public Void beforeCommit(TransactionData data, Transaction transaction, GraphDatabaseService databaseService) {
Node modifiedNode = data.assignedNodeProperties().iterator().next().entity();
assertEquals(node, modifiedNode);
modifiedNode.setProperty(key, value2);
return null;
}
};
dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
try (Transaction tx = db.beginTx()) {
// When
tx.getNodeById(node.getId()).setProperty(key, value1);
tx.commit();
}
// Then
try (Transaction transaction = db.beginTx()) {
var n = transaction.getNodeById(node.getId());
assertEquals(value2, n.getProperty(key));
}
dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
}
use of org.neo4j.graphdb.event.TransactionEventListenerAdapter in project neo4j by neo4j.
the class TestTransactionEvents method shouldAccessRelationshipDataInAfterCommit.
@Test
void shouldAccessRelationshipDataInAfterCommit() {
// GIVEN
final AtomicInteger accessCount = new AtomicInteger();
final Map<Long, RelationshipData> expectedRelationshipData = new HashMap<>();
TransactionEventListener<Void> listener = new TransactionEventListenerAdapter<>() {
@Override
public void afterCommit(TransactionData data, Void state, GraphDatabaseService databaseService) {
accessCount.set(0);
try (Transaction tx = db.beginTx()) {
for (Relationship relationship : data.createdRelationships()) {
accessData(tx.getRelationshipById(relationship.getId()));
}
for (PropertyEntry<Relationship> change : data.assignedRelationshipProperties()) {
accessData(tx.getRelationshipById(change.entity().getId()));
}
for (PropertyEntry<Relationship> change : data.removedRelationshipProperties()) {
accessData(change.entity());
}
tx.commit();
}
}
private void accessData(Relationship relationship) {
accessCount.incrementAndGet();
RelationshipData expectancy = expectedRelationshipData.get(relationship.getId());
assertNotNull(expectancy);
assertEquals(expectancy.startNode, relationship.getStartNode());
assertEquals(expectancy.type, relationship.getType().name());
assertEquals(expectancy.endNode, relationship.getEndNode());
}
};
dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
// WHEN
try {
Relationship relationship;
try (Transaction tx = db.beginTx()) {
relationship = tx.createNode().createRelationshipTo(tx.createNode(), MyRelTypes.TEST);
expectedRelationshipData.put(relationship.getId(), new RelationshipData(relationship));
tx.commit();
}
// THEN
assertEquals(1, accessCount.get());
// and WHEN
try (Transaction tx = db.beginTx()) {
relationship = tx.getRelationshipById(relationship.getId());
relationship.setProperty("name", "Smith");
Relationship otherRelationship = tx.createNode().createRelationshipTo(tx.createNode(), MyRelTypes.TEST2);
expectedRelationshipData.put(otherRelationship.getId(), new RelationshipData(otherRelationship));
tx.commit();
}
// THEN
assertEquals(2, accessCount.get());
// and WHEN
try (Transaction tx = db.beginTx()) {
tx.getRelationshipById(relationship.getId()).delete();
tx.commit();
}
// THEN
assertEquals(1, accessCount.get());
} finally {
dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
}
}
Aggregations