use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.
the class LegacyIndexProxy method putIfAbsent.
@Override
public T putIfAbsent(T entity, String key, Object value) {
try (Statement statement = statementContextBridge.get()) {
// Does it already exist?
long existing = single(internalGet(key, value, statement), -1L);
if (existing != -1) {
return entityOf(existing);
}
// No, OK so Grab lock
statement.readOperations().acquireExclusive(LEGACY_INDEX, legacyIndexResourceId(name, key));
// and check again -- now holding an exclusive lock
existing = single(internalGet(key, value, statement), -1L);
if (existing != -1) {
// Someone else created this entry before us just before we got the lock,
// release the lock as we won't be needing it
statement.readOperations().releaseExclusive(LEGACY_INDEX, legacyIndexResourceId(name, key));
return entityOf(existing);
}
internalAdd(entity, key, value, statement);
return null;
} catch (EntityNotFoundException e) {
throw new NotFoundException(format("%s %d not found", type, type.id(entity)), e);
} catch (InvalidTransactionTypeKernelException e) {
throw new ConstraintViolationException(e.getMessage(), e);
} catch (LegacyIndexNotFoundKernelException e) {
throw new RuntimeException(e);
}
}
use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.
the class UniquenessRecoveryTest method main.
/** This is the code that the test actually executes to attempt to violate the constraint. */
public static void main(String... args) throws Exception {
System.out.println("hello world");
File path = new File(args[0]);
boolean createConstraint = getBoolean("force_create_constraint") || !new File(path, "neostore").isFile();
GraphDatabaseService db = graphdb(path);
System.out.println("database started");
System.out.println("createConstraint = " + createConstraint);
if (createConstraint) {
try {
System.out.println("> creating constraint");
createConstraint(db);
System.out.println("< created constraint");
} catch (Exception e) {
System.out.println("!! failed to create constraint");
e.printStackTrace(System.out);
if (e instanceof ConstraintViolationException) {
System.out.println("... that is ok, since it means that constraint already exists ...");
} else {
System.exit(1);
}
}
}
try {
System.out.println("> adding node");
addNode(db);
System.out.println("< added node");
} catch (ConstraintViolationException e) {
System.out.println("!! failed to add node");
e.printStackTrace(System.out);
System.out.println("... this is probably what we want :) -- [but let's let the parent process verify]");
db.shutdown();
System.exit(0);
} catch (Exception e) {
System.out.println("!! failed to add node");
e.printStackTrace(System.out);
System.exit(2);
}
flushPageCache(db);
System.out.println("kill me");
await();
}
use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.
the class ConstraintCreationIT method shouldNotLeaveLuceneIndexFilesHangingAroundIfConstraintCreationFails.
@Test
public void shouldNotLeaveLuceneIndexFilesHangingAroundIfConstraintCreationFails() {
// given
GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
try (Transaction tx = db.beginTx()) {
for (int i = 0; i < 2; i++) {
Node node1 = db.createNode(LABEL);
node1.setProperty("prop", true);
}
tx.success();
}
// when
try (Transaction tx = db.beginTx()) {
db.schema().constraintFor(LABEL).assertPropertyIsUnique("prop").create();
fail("Should have failed with ConstraintViolationException");
tx.success();
} catch (ConstraintViolationException ignored) {
}
// then
try (Transaction ignore = db.beginTx()) {
assertEquals(0, Iterables.count(db.schema().getIndexes()));
}
SchemaIndexProvider schemaIndexProvider = db.getDependencyResolver().resolveDependency(SchemaIndexProvider.class);
File schemaStoreDir = schemaIndexProvider.getSchemaIndexStoreDirectory(new File(db.getStoreDir()));
assertFalse(new IndexFolderLayout(schemaStoreDir, INDEX_IDENTIFIER).getIndexFolder().exists());
}
use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.
the class ConstraintRecoveryIT method shouldNotHaveAnIndexIfUniqueConstraintCreationOnRecoveryFails.
@Test
public void shouldNotHaveAnIndexIfUniqueConstraintCreationOnRecoveryFails() throws IOException {
// given
final EphemeralFileSystemAbstraction fs = fileSystemRule.get();
fs.mkdir(new File("/tmp"));
File pathToDb = new File("/tmp/bar2");
TestGraphDatabaseFactory dbFactory = new TestGraphDatabaseFactory();
dbFactory.setFileSystem(fs);
final EphemeralFileSystemAbstraction[] storeInNeedOfRecovery = new EphemeralFileSystemAbstraction[1];
final AtomicBoolean monitorCalled = new AtomicBoolean(false);
Monitors monitors = new Monitors();
monitors.addMonitorListener(new IndexingService.MonitorAdapter() {
@Override
public void indexPopulationScanComplete() {
monitorCalled.set(true);
db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getSchemaStore().flush();
storeInNeedOfRecovery[0] = fs.snapshot();
}
});
dbFactory.setMonitors(monitors);
db = (GraphDatabaseAPI) dbFactory.newImpermanentDatabase(pathToDb);
try (Transaction tx = db.beginTx()) {
for (int i = 0; i < 2; i++) {
Node node1 = db.createNode(LABEL);
node1.setProperty("prop", true);
}
tx.success();
}
try (Transaction tx = db.beginTx()) {
db.schema().constraintFor(LABEL).assertPropertyIsUnique("prop").create();
fail("Should have failed with ConstraintViolationException");
tx.success();
} catch (ConstraintViolationException ignored) {
}
db.shutdown();
assertTrue(monitorCalled.get());
// when
dbFactory = new TestGraphDatabaseFactory();
dbFactory.setFileSystem(storeInNeedOfRecovery[0]);
db = (GraphDatabaseAPI) dbFactory.newImpermanentDatabase(pathToDb);
// then
try (Transaction tx = db.beginTx()) {
db.schema().awaitIndexesOnline(5000, TimeUnit.MILLISECONDS);
}
try (Transaction tx = db.beginTx()) {
assertEquals(2, Iterables.count(db.getAllNodes()));
}
try (Transaction tx = db.beginTx()) {
assertEquals(0, Iterables.count(Iterables.asList(db.schema().getConstraints())));
}
try (Transaction tx = db.beginTx()) {
assertEquals(0, Iterables.count(Iterables.asList(db.schema().getIndexes())));
}
db.shutdown();
}
use of org.neo4j.graphdb.ConstraintViolationException in project neo4j by neo4j.
the class SchemaIndexHaIT method creatingIndexOnSlaveIsNotAllowed.
@Test
public void creatingIndexOnSlaveIsNotAllowed() throws Throwable {
// GIVEN
ManagedCluster cluster = clusterRule.startCluster();
HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
// WHEN
try {
createIndex(slave);
fail("should have thrown exception");
} catch (ConstraintViolationException e) {
// expected
}
}
Aggregations