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