Search in sources :

Example 11 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class BatchInserterImpl method verifyIndexOrUniquenessConstraintCanBeCreated.

private void verifyIndexOrUniquenessConstraintCanBeCreated(int labelId, int[] propertyKeyIds, String errorMessage) {
    LabelSchemaDescriptor schemaDescriptor = SchemaDescriptorFactory.forLabel(labelId, propertyKeyIds);
    ConstraintDescriptor constraintDescriptor = ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKeyIds);
    if (schemaCache.hasIndexRule(schemaDescriptor) || schemaCache.hasConstraintRule(constraintDescriptor)) {
        throw new ConstraintViolationException(errorMessage);
    }
}
Also used : ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)

Example 12 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class PropertyConstraintsStressIT method performInserts.

/**
     * Inserts a bunch of new nodes with the label and property key currently set in the fields in this class, where
     * running this method twice will insert nodes with duplicate property values, assuming property key or label has
     * not changed.
     */
private WorkerCommand<Object, Integer> performInserts(final HighlyAvailableGraphDatabase slave, final boolean constraintCompliant) {
    return new WorkerCommand<Object, Integer>() {

        @Override
        public Integer doWork(Object state) throws Exception {
            int i = 0;
            try {
                for (; i < 100; i++) {
                    try (Transaction tx = slave.beginTx()) {
                        constraintOps.createEntity(slave, labelOrRelType, property, "value" + i, constraintCompliant);
                        tx.success();
                    }
                }
            } catch (TransactionFailureException | TransientTransactionFailureException e) {
            // Swallowed on purpose, we except it to fail sometimes due to either
            //  - constraint violation on master
            //  - concurrent schema operation on master
            } catch (ConstraintViolationException e) {
            // Constraint violation detected on slave while building transaction
            } catch (ComException e) {
            // Happens sometimes, cause:
            // - The lock session requested to start is already in use.
            //   Please retry your request in a few seconds.
            }
            return i;
        }
    };
}
Also used : ComException(org.neo4j.com.ComException) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException)

Example 13 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class NodeProxy method createRelationshipTo.

@Override
public Relationship createRelationshipTo(Node otherNode, RelationshipType type) {
    if (otherNode == null) {
        throw new IllegalArgumentException("Other node is null.");
    }
    //}
    try (Statement statement = actions.statement()) {
        int relationshipTypeId = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName(type.name());
        long relationshipId = statement.dataWriteOperations().relationshipCreate(relationshipTypeId, nodeId, otherNode.getId());
        return actions.newRelationshipProxy(relationshipId, nodeId, relationshipTypeId, otherNode.getId());
    } catch (IllegalTokenNameException | RelationshipTypeIdNotFoundKernelException e) {
        throw new IllegalArgumentException(e);
    } catch (EntityNotFoundException e) {
        throw new NotFoundException("Node[" + e.entityId() + "] is deleted and cannot be used to create a relationship");
    } catch (InvalidTransactionTypeKernelException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
}
Also used : Statement(org.neo4j.kernel.api.Statement) InvalidTransactionTypeKernelException(org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) RelationshipTypeIdNotFoundKernelException(org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) IllegalTokenNameException(org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException)

Example 14 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class TransactionConstraintsIT method createdSchemaConstraintsMustBeRetainedAcrossModeSwitches.

@Test
public void createdSchemaConstraintsMustBeRetainedAcrossModeSwitches() throws Throwable {
    // GIVEN
    // -- a node with a label and a property, and a constraint on those
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    createConstraint(master, LABEL, PROPERTY_KEY);
    createNode(master, PROPERTY_VALUE, LABEL).getId();
    // WHEN
    cluster.sync();
    ClusterManager.RepairKit originalMasterRepairKit = cluster.fail(master);
    cluster.await(masterAvailable(master));
    takeTheLeadInAnEventualMasterSwitch(cluster.getMaster());
    cluster.sync();
    originalMasterRepairKit.repair();
    cluster.await(allSeesAllAsAvailable());
    cluster.sync();
    // THEN the constraints should still be in place and enforced
    int i = 0;
    for (HighlyAvailableGraphDatabase instance : cluster.getAllMembers()) {
        try {
            createNode(instance, PROPERTY_VALUE, LABEL);
            fail("Node with " + PROPERTY_VALUE + " should already exist");
        } catch (ConstraintViolationException e) {
        // Good, this node should already exist
        }
        for (int p = 0; p < i - 1; p++) {
            try {
                createNode(instance, PROPERTY_VALUE + String.valueOf(p), LABEL);
                fail("Node with " + PROPERTY_VALUE + String.valueOf(p) + " should already exist");
            } catch (ConstraintViolationException e) {
            // Good
            }
        }
        createNode(instance, PROPERTY_VALUE + String.valueOf(i), LABEL);
        i++;
    }
}
Also used : HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) ClusterManager(org.neo4j.kernel.impl.ha.ClusterManager) Test(org.junit.Test)

Example 15 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowDuplicatedIndexes.

@Test
public void shouldNotAllowDuplicatedIndexes() throws Exception {
    // Given
    Label label = label("Person3-" + denseNodeThreshold);
    String property = "name";
    BatchInserter inserter = globalInserter;
    // When
    inserter.createDeferredSchemaIndex(label).on(property).create();
    try {
        inserter.createDeferredSchemaIndex(label).on(property).create();
        fail("Exception expected");
    } catch (ConstraintViolationException e) {
        // Then
        assertEquals("Index for given {label;property} already exists", e.getMessage());
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Aggregations

ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)21 Test (org.junit.Test)14 BatchInserter (org.neo4j.unsafe.batchinsert.BatchInserter)8 Transaction (org.neo4j.graphdb.Transaction)6 Label (org.neo4j.graphdb.Label)4 File (java.io.File)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Node (org.neo4j.graphdb.Node)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3 Statement (org.neo4j.kernel.api.Statement)3 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)3 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)3 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)2 IllegalTokenNameException (org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException)2 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ComException (org.neo4j.com.ComException)1 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)1 TransientTransactionFailureException (org.neo4j.graphdb.TransientTransactionFailureException)1