use of org.neo4j.graphdb.TransactionFailureException in project neo4j by neo4j.
the class DbRepresentation method of.
public static DbRepresentation of(GraphDatabaseService db) {
int retryCount = 30;
while (true) {
try (Transaction transaction = db.beginTx()) {
var schema = transaction.schema();
schema.awaitIndexesOnline(1, TimeUnit.MINUTES);
DbRepresentation result = new DbRepresentation();
for (Node node : transaction.getAllNodes()) {
NodeRep nodeRep = new NodeRep(node);
result.nodes.put(node.getId(), nodeRep);
result.highestNodeId = Math.max(node.getId(), result.highestNodeId);
result.highestRelationshipId = Math.max(nodeRep.highestRelationshipId, result.highestRelationshipId);
}
for (IndexDefinition indexDefinition : schema.getIndexes()) {
result.schemaIndexes.add(indexDefinition);
}
for (ConstraintDefinition constraintDefinition : schema.getConstraints()) {
result.constraints.add(constraintDefinition);
}
return result;
} catch (TransactionFailureException | DatabaseShutdownException e) {
if (retryCount-- < 0) {
throw e;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(e);
}
}
}
}
use of org.neo4j.graphdb.TransactionFailureException in project neo4j by neo4j.
the class TestTransactionEvents method makeSureBeforeAfterAreCalledCorrectly.
@Test
void makeSureBeforeAfterAreCalledCorrectly() {
List<TransactionEventListener<Object>> listeners = new ArrayList<>();
listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), false));
listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), false));
listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), true));
listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), false));
for (TransactionEventListener<Object> listener : listeners) {
dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
}
try {
Transaction tx = db.beginTx();
try {
tx.createNode().delete();
tx.commit();
fail("Should fail commit");
} catch (TransactionFailureException e) {
// OK
}
verifyListenerCalls(listeners, false);
dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listeners.remove(2));
for (TransactionEventListener<Object> listener : listeners) {
((DummyTransactionEventListener<Object>) ((FailingEventListener<Object>) listener).source).reset();
}
try (Transaction transaction = db.beginTx()) {
tx.createNode().delete();
transaction.commit();
}
verifyListenerCalls(listeners, true);
} finally {
for (TransactionEventListener<Object> listener : listeners) {
dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
}
}
}
use of org.neo4j.graphdb.TransactionFailureException in project neo4j by neo4j.
the class TestTransactionEvents method shouldBeAbleToAccessExceptionThrownInEventHook.
@Test
void shouldBeAbleToAccessExceptionThrownInEventHook() {
class MyFancyException extends Exception {
}
ExceptionThrowingEventListener listener = new ExceptionThrowingEventListener(new MyFancyException(), null, null);
dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
try {
Transaction tx = db.beginTx();
try {
tx.createNode().delete();
tx.commit();
fail("Should fail commit");
} catch (TransactionFailureException e) {
Throwable currentEx = e;
do {
currentEx = currentEx.getCause();
if (currentEx instanceof MyFancyException) {
return;
}
} while (currentEx.getCause() != null);
fail("Expected to find the exception thrown in the event hook as the cause of transaction failure.");
}
} finally {
dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
}
}
use of org.neo4j.graphdb.TransactionFailureException in project neo4j by neo4j.
the class ForsetiClient method tryUpgradeToExclusiveWithShareLockHeld.
/**
* Attempt to upgrade a share lock that we hold to an exclusive lock.
*/
private boolean tryUpgradeToExclusiveWithShareLockHeld(LockTracer tracer, LockWaitEvent priorEvent, ResourceType resourceType, long resourceId, SharedLock sharedLock, int tries, long waitStartNano) throws AcquireLockTimeoutException {
if (sharedLock.tryAcquireUpdateLock(this)) {
LockWaitEvent waitEvent = null;
try {
// Now we just wait for all clients to release the the share lock
while (sharedLock.numberOfHolders() > 1) {
assertValid(waitStartNano, resourceType, resourceId);
if (waitEvent == null && priorEvent == null) {
waitEvent = tracer.waitForLock(EXCLUSIVE, resourceType, userTransactionId, resourceId);
}
waitFor(sharedLock, resourceType, resourceId, tries++);
}
return true;
} catch (Throwable e) {
sharedLock.releaseUpdateLock();
if (e instanceof DeadlockDetectedException || e instanceof LockClientStoppedException) {
throw (RuntimeException) e;
}
throw new TransactionFailureException("Failed to upgrade shared lock to exclusive: " + sharedLock, e);
} finally {
if (waitEvent != null) {
waitEvent.close();
}
clearWaitList();
waitingForLock = null;
}
}
return false;
}
use of org.neo4j.graphdb.TransactionFailureException in project neo4j by neo4j.
the class RelationshipEntity method setProperty.
@Override
public void setProperty(String key, Object value) {
KernelTransaction transaction = internalTransaction.kernelTransaction();
int propertyKeyId;
try {
propertyKeyId = transaction.tokenWrite().propertyKeyGetOrCreateForName(key);
} catch (IllegalTokenNameException e) {
throw new IllegalArgumentException(format("Invalid property key '%s'.", key), e);
} catch (TokenCapacityExceededKernelException e) {
throw new ConstraintViolationException(e.getMessage(), e);
} catch (KernelException e) {
throw new TransactionFailureException("Unknown error trying to create property key token", e);
}
try {
transaction.dataWrite().relationshipSetProperty(id, propertyKeyId, Values.of(value, false));
} catch (IllegalArgumentException e) {
try {
transaction.rollback();
} catch (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException ex) {
ex.addSuppressed(e);
throw new TransactionFailureException("Fail to rollback transaction.", ex);
}
throw e;
} catch (EntityNotFoundException e) {
throw new NotFoundException(e);
} catch (InvalidTransactionTypeKernelException e) {
throw new ConstraintViolationException(e.getMessage(), e);
}
}
Aggregations