Search in sources :

Example 1 with CypherException

use of org.neo4j.ogm.exception.CypherException in project neo4j-ogm by neo4j.

the class BoltRequest method execute.

@Override
public Response<RowModel> execute(DefaultRequest query) {
    final List<RowModel> rowModels = new ArrayList<>();
    String[] columns = null;
    for (Statement statement : query.getStatements()) {
        Result result = executeRequest(statement);
        if (columns == null) {
            try {
                List<String> columnSet = result.keys();
                columns = columnSet.toArray(new String[columnSet.size()]);
            } catch (ClientException e) {
                throw new CypherException(e.code(), e.getMessage(), e);
            }
        }
        try (RowModelResponse rowModelResponse = new RowModelResponse(result, entityAdapter)) {
            RowModel model;
            while ((model = rowModelResponse.next()) != null) {
                rowModels.add(model);
            }
            result.consume();
        } catch (ClientException e) {
            throw new CypherException(e.code(), e.getMessage(), e);
        }
    }
    return new MultiStatementBasedResponse(columns, rowModels);
}
Also used : RowModelResponse(org.neo4j.ogm.drivers.bolt.response.RowModelResponse) GraphRowModelResponse(org.neo4j.ogm.drivers.bolt.response.GraphRowModelResponse) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) RowModel(org.neo4j.ogm.model.RowModel) ClientException(org.neo4j.driver.exceptions.ClientException) CypherException(org.neo4j.ogm.exception.CypherException) Result(org.neo4j.driver.Result)

Example 2 with CypherException

use of org.neo4j.ogm.exception.CypherException in project neo4j-ogm by neo4j.

the class BoltRequest method executeRequest.

private Result executeRequest(Statement request) {
    try {
        Map<String, Object> parameterMap = this.parameterConversion.convertParameters(request.getParameters());
        String cypher = cypherModification.apply(request.getStatement());
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Request: {} with params {}", cypher, parameterMap);
        }
        BoltTransaction tx = (BoltTransaction) transaction;
        return tx.nativeBoltTransaction().run(cypher, parameterMap);
    } catch (ClientException | DatabaseException | TransientException ce) {
        throw new CypherException(ce.code(), ce.getMessage(), ce);
    }
}
Also used : BoltTransaction(org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction) TransientException(org.neo4j.driver.exceptions.TransientException) ClientException(org.neo4j.driver.exceptions.ClientException) CypherException(org.neo4j.ogm.exception.CypherException) DatabaseException(org.neo4j.driver.exceptions.DatabaseException)

Example 3 with CypherException

use of org.neo4j.ogm.exception.CypherException in project neo4j-ogm by neo4j.

the class BoltTransaction method commit.

@Override
public void commit() {
    final boolean canCommit = transactionManager.canCommit();
    try {
        if (canCommit) {
            LOGGER.debug("Committing native transaction: {}", nativeTransaction);
            if (nativeTransaction.isOpen()) {
                nativeTransaction.commit();
                nativeTransaction.close();
                nativeSession.close();
            } else {
                throw new IllegalStateException("Transaction is already closed");
            }
        }
    } catch (ClientException ce) {
        closeNativeSessionIfPossible();
        if (ce.code().startsWith(NEO_CLIENT_ERROR_SECURITY)) {
            throw new ConnectionException("Security Error: " + ce.code() + ", " + ce.getMessage(), ce);
        }
        throw new CypherException(ce.code(), ce.getMessage(), ce);
    } catch (Exception e) {
        closeNativeSessionIfPossible();
        throw new TransactionException(e.getLocalizedMessage(), e);
    } finally {
        super.commit();
        if (canCommit) {
            Bookmark bookmark = nativeSession.lastBookmark();
            if (bookmark != null) {
                String bookmarkAsString = String.join(BOOKMARK_SEPARATOR, ((InternalBookmark) bookmark).values());
                transactionManager.bookmark(bookmarkAsString);
            }
        }
    }
}
Also used : TransactionException(org.neo4j.ogm.exception.TransactionException) InternalBookmark(org.neo4j.driver.internal.InternalBookmark) Bookmark(org.neo4j.driver.Bookmark) ClientException(org.neo4j.driver.exceptions.ClientException) CypherException(org.neo4j.ogm.exception.CypherException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) CypherException(org.neo4j.ogm.exception.CypherException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) TransactionException(org.neo4j.ogm.exception.TransactionException) ClientException(org.neo4j.driver.exceptions.ClientException)

Example 4 with CypherException

use of org.neo4j.ogm.exception.CypherException in project neo4j-ogm by neo4j.

the class EmbeddedRequest method executeRequest.

private Result executeRequest(Statement request) {
    try {
        Map<String, Object> parameterMap = this.parameterConversion.convertParameters(request.getParameters());
        String cypher = cypherModification.apply(request.getStatement());
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Request: {} with params {}", cypher, parameterMap);
        }
        if (this.transaction == null) {
            throw new RuntimeException("Cannot execute request without transaction!");
        } else {
            return ((EmbeddedTransaction) transaction).getNativeTransaction().execute(cypher, parameterMap);
        }
    } catch (QueryExecutionException qee) {
        throw new CypherException(qee.getStatusCode(), qee.getMessage(), qee);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) CypherException(org.neo4j.ogm.exception.CypherException) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) CypherException(org.neo4j.ogm.exception.CypherException)

Example 5 with CypherException

use of org.neo4j.ogm.exception.CypherException in project neo4j-ogm by neo4j.

the class Neo4jSession method doInTransaction.

/**
 * For internal use only. Opens a new transaction if necessary before running statements
 * in case an explicit transaction does not exist. It is designed to be the central point
 * for handling exceptions coming from the DB and apply commit / rollback rules.
 *
 * @param function The callback to execute.
 * @param <T>      The result type.
 * @param txType   Transaction type, readonly or not.
 * @return The result of the transaction function.
 */
public <T> T doInTransaction(TransactionalUnitOfWork<T> function, boolean forceTx, Transaction.Type txType) {
    Transaction transaction = txManager.getCurrentTransaction();
    // If we (force) create a new transaction, we are in charge of handling rollback in case of errors
    // and cleaning up afterwards.
    boolean newTransaction = false;
    try {
        if (forceTx || (driver.requiresTransaction() && transaction == null)) {
            transaction = beginTransaction(txType);
            newTransaction = true;
        }
        T result = function.doInTransaction();
        if (newTransaction && txManager.canCommit()) {
            transaction.commit();
        }
        return result;
    } catch (CypherException e) {
        if (newTransaction && txManager.canRollback()) {
            logger.warn("Error executing query : {} - {}. Rolling back transaction.", e.getCode(), e.getDescription());
            transaction.rollback();
        }
        throw e;
    } catch (Throwable e) {
        if (newTransaction && txManager.canRollback()) {
            logger.warn("Error executing query : {}. Rolling back transaction.", e.getMessage());
            transaction.rollback();
        }
        throw driver.getExceptionTranslator().translateExceptionIfPossible(e);
    } finally {
        if (newTransaction && transaction != null && !transaction.status().equals(Transaction.Status.CLOSED)) {
            transaction.close();
        }
    }
}
Also used : Transaction(org.neo4j.ogm.transaction.Transaction) CypherException(org.neo4j.ogm.exception.CypherException)

Aggregations

CypherException (org.neo4j.ogm.exception.CypherException)8 ClientException (org.neo4j.driver.exceptions.ClientException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 Test (org.junit.Test)1 Bookmark (org.neo4j.driver.Bookmark)1 Result (org.neo4j.driver.Result)1 DatabaseException (org.neo4j.driver.exceptions.DatabaseException)1 TransientException (org.neo4j.driver.exceptions.TransientException)1 InternalBookmark (org.neo4j.driver.internal.InternalBookmark)1 QueryExecutionException (org.neo4j.graphdb.QueryExecutionException)1 User (org.neo4j.ogm.domain.social.User)1 GraphRowModelResponse (org.neo4j.ogm.drivers.bolt.response.GraphRowModelResponse)1 RowModelResponse (org.neo4j.ogm.drivers.bolt.response.RowModelResponse)1 BoltTransaction (org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction)1 HttpRequestException (org.neo4j.ogm.drivers.http.request.HttpRequestException)1 ConnectionException (org.neo4j.ogm.exception.ConnectionException)1