use of org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException in project neo4j by neo4j.
the class KernelTransactionImplementation method commitTransaction.
private long commitTransaction() throws KernelException {
boolean success = false;
long txId = READ_ONLY_ID;
TransactionListenersState listenersState = null;
try (CommitEvent commitEvent = transactionEvent.beginCommitEvent()) {
listenersState = eventListeners.beforeCommit(txState, this, storageReader);
if (listenersState != null && listenersState.isFailed()) {
Throwable cause = listenersState.failure();
if (cause instanceof TransientFailureException) {
throw (TransientFailureException) cause;
}
if (cause instanceof Status.HasStatus) {
throw new TransactionFailureException(((Status.HasStatus) cause).status(), cause, cause.getMessage());
}
throw new TransactionFailureException(Status.Transaction.TransactionHookFailed, cause, cause.getMessage());
}
// Convert changes into commands and commit
if (hasChanges()) {
forceThawLocks();
lockClient.prepareForCommit();
// Gather up commands from the various sources
HeapTrackingArrayList<StorageCommand> extractedCommands = HeapTrackingCollections.newArrayList(memoryTracker);
storageEngine.createCommands(extractedCommands, txState, storageReader, commandCreationContext, lockClient, lockTracer(), lastTransactionIdWhenStarted, this::enforceConstraints, cursorContext, memoryTracker);
/* Here's the deal: we track a quick-to-access hasChanges in transaction state which is true
* if there are any changes imposed by this transaction. Some changes made inside a transaction undo
* previously made changes in that same transaction, and so at some point a transaction may have
* changes and at another point, after more changes seemingly,
* the transaction may not have any changes.
* However, to track that "undoing" of the changes is a bit tedious, intrusive and hard to maintain
* and get right.... So to really make sure the transaction has changes we re-check by looking if we
* have produced any commands to add to the logical log.
*/
if (!extractedCommands.isEmpty()) {
// Finish up the whole transaction representation
PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(extractedCommands);
long timeCommitted = clocks.systemClock().millis();
transactionRepresentation.setHeader(EMPTY_BYTE_ARRAY, startTimeMillis, lastTransactionIdWhenStarted, timeCommitted, leaseClient.leaseId(), securityContext.subject());
// Commit the transaction
success = true;
TransactionToApply batch = new TransactionToApply(transactionRepresentation, cursorContext);
kernelTransactionMonitor.beforeApply();
txId = commitProcess.commit(batch, commitEvent, INTERNAL);
commitTime = timeCommitted;
}
}
success = true;
return txId;
} catch (ConstraintValidationException | CreateConstraintFailureException e) {
throw new ConstraintViolationTransactionFailureException(e.getUserMessage(tokenRead()), e);
} finally {
if (!success) {
rollback(listenersState);
} else {
transactionId = txId;
afterCommit(listenersState);
}
transactionMonitor.addHeapTransactionSize(memoryTracker.heapHighWaterMark());
transactionMonitor.addNativeTransactionSize(memoryTracker.usedNativeMemory());
}
}
Aggregations