Search in sources :

Example 11 with TransactionFailureException

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);
            }
        }
    }
}
Also used : Node(org.neo4j.graphdb.Node) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition)

Example 12 with TransactionFailureException

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);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) ArrayList(java.util.ArrayList) TransactionEventListener(org.neo4j.graphdb.event.TransactionEventListener) InternalTransactionEventListener(org.neo4j.kernel.internal.event.InternalTransactionEventListener) Test(org.junit.jupiter.api.Test)

Example 13 with TransactionFailureException

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);
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) Test(org.junit.jupiter.api.Test)

Example 14 with TransactionFailureException

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;
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) LockClientStoppedException(org.neo4j.kernel.impl.locking.LockClientStoppedException) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) LockWaitEvent(org.neo4j.lock.LockWaitEvent)

Example 15 with TransactionFailureException

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);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) TokenCapacityExceededKernelException(org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) IllegalTokenNameException(org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) PropertyKeyIdNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) TokenCapacityExceededKernelException(org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException) KernelException(org.neo4j.exceptions.KernelException)

Aggregations

TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)56 Transaction (org.neo4j.graphdb.Transaction)16 NotFoundException (org.neo4j.graphdb.NotFoundException)9 Test (org.junit.Test)7 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)7 Node (org.neo4j.graphdb.Node)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 LinkedList (java.util.LinkedList)6 SystemException (javax.transaction.SystemException)6 XAException (javax.transaction.xa.XAException)6 XAResource (javax.transaction.xa.XAResource)6 KernelException (org.neo4j.exceptions.KernelException)6 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)6 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)6 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)6 IllegalTokenNameException (org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException)6 TokenCapacityExceededKernelException (org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException)6 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 IOException (java.io.IOException)5