Database Reference
In-Depth Information
Listing 7.2. Attempting to update with a transaction
This listing represents the general pattern of starting a transaction, carrying out some op-
erations, and assuming they complete without exception. After the success method is
called and the code block completes, the changes are committed to the database.
Schema-based operations and transactions
If you're performing any schema-related activities within the graph, this code will need to
be in a separate transaction from all other Neo4j transactional code; otherwise, an excep-
tionoftheform ConstraintViolationException willbethrownwiththemessage
“Cannot perform data updates in a transaction that has performed schema updates.”
Schema indexing was covered in chapter 5 , and the following snippet is a partial copy of
listing 5.5 to demonstrate this case. Torecap, you're defining name as a schema-indexable
property on the USER label in one transaction, and then using a separate transaction to ac-
tually set the value on a real user node:
Label userLabel = DynamicLabel.label("USER");
// 1. Do schema related work in one transaction.
try (Transaction tx = graphDb.beginTx()) {
graphDb.schema().indexFor(userLabel).on("name").create();
tx.success();
}
// 2. Do other work in a separate transaction.
Node user = null;
try (Transaction tx = graphDb.beginTx()) {
user = graphDb.createNode(userLabel);
user.setProperty("name", "Michael Collins");
tx.success();
}}
Search WWH ::




Custom Search