Search in sources :

Example 36 with TransactionFailureException

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

the class ServerBootstrapper method start.

@Override
@SafeVarargs
public final int start(File homeDir, Optional<File> configFile, Pair<String, String>... configOverrides) {
    addShutdownHook();
    try {
        Config config = createConfig(homeDir, configFile, configOverrides);
        LogProvider userLogProvider = setupLogging(config);
        dependencies = dependencies.userLogProvider(userLogProvider);
        log = userLogProvider.getLog(getClass());
        config.setLogger(log);
        serverAddress = config.httpConnectors().stream().filter(c -> Encryption.NONE.equals(c.encryptionLevel())).findFirst().map((connector) -> config.get(connector.listen_address).toString()).orElse(serverAddress);
        checkCompatibility();
        server = createNeoServer(config, dependencies, userLogProvider);
        server.start();
        return OK;
    } catch (ServerStartupException e) {
        e.describeTo(log);
        return WEB_SERVER_STARTUP_ERROR_CODE;
    } catch (TransactionFailureException tfe) {
        String locationMsg = (server == null) ? "" : " Another process may be using database location " + server.getDatabase().getLocation();
        log.error(format("Failed to start Neo4j on %s.", serverAddress) + locationMsg, tfe);
        return GRAPH_DATABASE_STARTUP_ERROR_CODE;
    } catch (Exception e) {
        log.error(format("Failed to start Neo4j on %s.", serverAddress), e);
        return WEB_SERVER_STARTUP_ERROR_CODE;
    }
}
Also used : LogProvider(org.neo4j.logging.LogProvider) FormattedLogProvider(org.neo4j.logging.FormattedLogProvider) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Config(org.neo4j.kernel.configuration.Config) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Example 37 with TransactionFailureException

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

the class LifecycleManagingDatabaseTest method mustIgnoreExceptionsFromPreLoadingCypherQuery.

@Test
public void mustIgnoreExceptionsFromPreLoadingCypherQuery() throws Throwable {
    // Given a lifecycled database that'll try to warm up Cypher when it starts
    final GraphDatabaseFacade mockDb = mock(GraphDatabaseFacade.class);
    Config config = Config.empty();
    GraphDatabaseFacadeFactory.Dependencies deps = GraphDatabaseDependencies.newDependencies().userLogProvider(NullLogProvider.getInstance());
    LifecycleManagingDatabase.GraphFactory factory = (conf, dependencies) -> mockDb;
    LifecycleManagingDatabase db = new LifecycleManagingDatabase(config, factory, deps) {

        @Override
        protected boolean isInTestMode() {
            return false;
        }
    };
    // When the execution of the query fails (for instance when this is a slave that just joined a cluster and is
    // working on catching up to the master)
    when(mockDb.execute(LifecycleManagingDatabase.CYPHER_WARMUP_QUERY)).thenThrow(new TransactionFailureException("Boo"));
    // Then the database should still start up as normal, without bubbling the exception up
    db.init();
    db.start();
    assertTrue("the database should be running", db.isRunning());
    db.stop();
    db.shutdown();
}
Also used : GraphDatabaseDependencies(org.neo4j.kernel.GraphDatabaseDependencies) Config(org.neo4j.kernel.configuration.Config) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) GraphDatabaseFacadeFactory(org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Mockito.when(org.mockito.Mockito.when) NullLogProvider(org.neo4j.logging.NullLogProvider) Mockito.mock(org.mockito.Mockito.mock) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Config(org.neo4j.kernel.configuration.Config) GraphDatabaseFacadeFactory(org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) Test(org.junit.Test)

Example 38 with TransactionFailureException

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

the class DatabaseRecoveryIT method recoveryShouldFixPartiallyAppliedSchemaIndexUpdates.

@Test
void recoveryShouldFixPartiallyAppliedSchemaIndexUpdates() {
    Label label = Label.label("Foo");
    String property = "Bar";
    // cause failure during 'relationship.delete()' command application
    ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, true), Command.RelationshipCommand.class);
    adversary.disable();
    Path storeDir = directory.homePath();
    DatabaseManagementService managementService = AdversarialPageCacheGraphDatabaseFactory.create(storeDir, fileSystem, adversary).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    try {
        try (Transaction tx = db.beginTx()) {
            tx.schema().constraintFor(label).assertPropertyIsUnique(property).create();
            tx.commit();
        }
        long relationshipId = createRelationship(db);
        TransactionFailureException txFailure = null;
        try (Transaction tx = db.beginTx()) {
            Node node = tx.createNode(label);
            node.setProperty(property, "B");
            // this should fail because of the adversary
            tx.getRelationshipById(relationshipId).delete();
            adversary.enable();
            tx.commit();
        } catch (TransactionFailureException e) {
            txFailure = e;
        }
        assertNotNull(txFailure);
        adversary.disable();
        // heal the db so it is possible to inspect the data
        healthOf(db).healed();
        // now we can observe partially committed state: node is in the index and relationship still present
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(label, property, "B", tx));
            assertNotNull(tx.getRelationshipById(relationshipId));
            tx.commit();
        }
        // panic the db again to force recovery on the next startup
        healthOf(db).panic(txFailure.getCause());
        // restart the database, now with regular page cache
        managementService.shutdown();
        db = startDatabase(storeDir);
        // now we observe correct state: node is in the index and relationship is removed
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(label, property, "B", tx));
            assertRelationshipNotExist(tx, relationshipId);
            tx.commit();
        }
    } finally {
        managementService.shutdown();
    }
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CountingAdversary(org.neo4j.adversaries.CountingAdversary) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Transaction(org.neo4j.graphdb.Transaction) Command(org.neo4j.internal.recordstorage.Command) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 39 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class LockReleaser method releaseCows.

void releaseCows(Transaction cowTxId, int param) {
    PrimitiveElement element = cowMap.remove(cowTxId);
    if (element == null) {
        return;
    }
    ArrayMap<Long, CowNodeElement> cowNodeElements = element.nodes;
    Set<Entry<Long, CowNodeElement>> nodeEntrySet = cowNodeElements.entrySet();
    for (Entry<Long, CowNodeElement> entry : nodeEntrySet) {
        NodeImpl node = nodeManager.getNodeIfCached(entry.getKey());
        if (node != null) {
            CowNodeElement nodeElement = entry.getValue();
            if (param == Status.STATUS_COMMITTED) {
                node.commitRelationshipMaps(nodeElement.relationshipAddMap, nodeElement.relationshipRemoveMap);
                node.commitPropertyMaps(nodeElement.propertyAddMap, nodeElement.propertyRemoveMap);
            } else if (param != Status.STATUS_ROLLEDBACK) {
                throw new TransactionFailureException("Unknown transaction status: " + param);
            }
        }
    }
    ArrayMap<Long, CowRelElement> cowRelElements = element.relationships;
    Set<Entry<Long, CowRelElement>> relEntrySet = cowRelElements.entrySet();
    for (Entry<Long, CowRelElement> entry : relEntrySet) {
        RelationshipImpl rel = nodeManager.getRelIfCached(entry.getKey());
        if (rel != null) {
            CowRelElement relElement = entry.getValue();
            if (param == Status.STATUS_COMMITTED) {
                rel.commitPropertyMaps(relElement.propertyAddMap, relElement.propertyRemoveMap);
            } else if (param != Status.STATUS_ROLLEDBACK) {
                throw new TransactionFailureException("Unknown transaction status: " + param);
            }
        }
    }
    cowMap.remove(cowTxId);
}
Also used : Entry(java.util.Map.Entry) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Example 40 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class LockReleaser method addLockToTransaction.

/**
 * Invoking this method with no transaction running will cause the lock to
 * be released right away.
 *
 * @param resource
 *            the resource on which the lock is taken
 * @param type
 *            type of lock (READ or WRITE)
 * @throws NotInTransactionException
 */
public void addLockToTransaction(Object resource, LockType type) throws NotInTransactionException {
    Transaction tx = getTransaction();
    List<LockElement> lockElements = lockMap.get(tx);
    if (lockElements != null) {
        lockElements.add(new LockElement(resource, type));
    } else {
        if (tx == null) {
            // no transaction we release lock right away
            if (type == LockType.WRITE) {
                lockManager.releaseWriteLock(resource, null);
            } else if (type == LockType.READ) {
                lockManager.releaseReadLock(resource, null);
            }
            return;
        }
        lockElements = new ArrayList<LockElement>();
        lockMap.put(tx, lockElements);
        lockElements.add(new LockElement(resource, type));
        // tx was read only
        try {
            tx.registerSynchronization(new ReadOnlyTxReleaser(tx));
        } catch (Exception e) {
            throw new TransactionFailureException("Failed to register lock release synchronization hook", e);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

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