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);
}
}
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;
}
};
}
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);
}
}
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++;
}
}
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());
}
}
Aggregations