use of com.yahoo.elide.core.exceptions.TransactionException in project elide by yahoo.
the class AbstractJpaTransaction method flush.
@Override
public void flush(RequestScope requestScope) {
if (!isOpen()) {
return;
}
try {
deferredTasks.forEach(Runnable::run);
deferredTasks.clear();
FlushModeType flushMode = em.getFlushMode();
if (flushMode == FlushModeType.AUTO && isOpen()) {
em.flush();
}
} catch (Exception e) {
try {
rollback();
} catch (RuntimeException e2) {
e.addSuppressed(e2);
} finally {
log.error("Caught entity manager exception during flush", e);
}
if (e instanceof ConstraintViolationException) {
throw e;
}
throw new TransactionException(e);
}
}
use of com.yahoo.elide.core.exceptions.TransactionException in project faf-java-api by FAForever.
the class SpringHibernateTransaction method commit.
@Override
public void commit(RequestScope scope) {
try {
flush(scope);
txManager.commit(txStatus);
} catch (org.springframework.transaction.TransactionException e) {
throw new TransactionException(e);
}
}
use of com.yahoo.elide.core.exceptions.TransactionException in project elide by yahoo.
the class HashMapStoreTransaction method createObject.
@Override
public void createObject(Object entity, RequestScope scope) {
Type entityClass = EntityDictionary.getType(entity);
String idFieldName = dictionary.getIdFieldName(entityClass);
String id;
if (containsObject(entity)) {
throw new TransactionException(new IllegalStateException("Duplicate key"));
}
// GeneratedValue means the DB needs to assign the ID.
if (dictionary.getAttributeOrRelationAnnotation(entityClass, GeneratedValue.class, idFieldName) != null) {
// TODO: Id's are not necessarily numeric.
AtomicLong nextId;
synchronized (dataStore) {
nextId = getId(entityClass);
}
id = String.valueOf(nextId.getAndIncrement());
setId(entity, id);
} else {
id = dictionary.getId(entity);
}
replicateOperationToParent(entity, Operation.OpType.CREATE);
operations.add(new Operation(id, entity, EntityDictionary.getType(entity), Operation.OpType.CREATE));
}
use of com.yahoo.elide.core.exceptions.TransactionException in project elide by yahoo.
the class Elide method handleRequest.
/**
* Handle JSON API requests.
*
* @param isReadOnly if the transaction is read only
* @param user the user object from the container
* @param transaction a transaction supplier
* @param requestId the Request ID
* @param handler a function that creates the request scope and request handler
* @return the response
*/
protected ElideResponse handleRequest(boolean isReadOnly, User user, Supplier<DataStoreTransaction> transaction, UUID requestId, Handler<DataStoreTransaction, User, HandlerResult> handler) {
boolean isVerbose = false;
try (DataStoreTransaction tx = transaction.get()) {
transactionRegistry.addRunningTransaction(requestId, tx);
HandlerResult result = handler.handle(tx, user);
RequestScope requestScope = result.getRequestScope();
isVerbose = requestScope.getPermissionExecutor().isVerbose();
Supplier<Pair<Integer, JsonNode>> responder = result.getResponder();
tx.preCommit(requestScope);
requestScope.runQueuedPreSecurityTriggers();
requestScope.getPermissionExecutor().executeCommitChecks();
requestScope.runQueuedPreFlushTriggers();
if (!isReadOnly) {
requestScope.saveOrCreateObjects();
}
tx.flush(requestScope);
requestScope.runQueuedPreCommitTriggers();
ElideResponse response = buildResponse(responder.get());
auditLogger.commit();
tx.commit(requestScope);
requestScope.runQueuedPostCommitTriggers();
if (log.isTraceEnabled()) {
requestScope.getPermissionExecutor().logCheckStats();
}
return response;
} catch (JacksonException e) {
String message = (e.getLocation() != null && e.getLocation().getSourceRef() != null) ? // This will leak Java class info if the location isn't known.
e.getMessage() : e.getOriginalMessage();
return buildErrorResponse(new BadRequestException(message), isVerbose);
} catch (IOException e) {
log.error("IO Exception uncaught by Elide", e);
return buildErrorResponse(new TransactionException(e), isVerbose);
} catch (RuntimeException e) {
return handleRuntimeException(e, isVerbose);
} finally {
transactionRegistry.removeRunningTransaction(requestId);
auditLogger.clear();
}
}
use of com.yahoo.elide.core.exceptions.TransactionException in project elide by yahoo.
the class QueryRunner method executeGraphQLRequest.
private ElideResponse executeGraphQLRequest(String baseUrlEndPoint, ObjectMapper mapper, User principal, String graphQLDocument, GraphQLQuery query, UUID requestId, Map<String, List<String>> requestHeaders) {
boolean isVerbose = false;
String queryText = query.getQuery();
boolean isMutation = isMutation(queryText);
try (DataStoreTransaction tx = isMutation ? elide.getDataStore().beginTransaction() : elide.getDataStore().beginReadTransaction()) {
elide.getTransactionRegistry().addRunningTransaction(requestId, tx);
if (query.getQuery() == null || query.getQuery().isEmpty()) {
return ElideResponse.builder().responseCode(HttpStatus.SC_BAD_REQUEST).body("A `query` key is required.").build();
}
// get variables from request for constructing entityProjections
Map<String, Object> variables = query.getVariables();
// TODO - get API version.
GraphQLProjectionInfo projectionInfo = new GraphQLEntityProjectionMaker(elide.getElideSettings(), variables, apiVersion).make(queryText);
GraphQLRequestScope requestScope = new GraphQLRequestScope(baseUrlEndPoint, tx, principal, apiVersion, elide.getElideSettings(), projectionInfo, requestId, requestHeaders);
isVerbose = requestScope.getPermissionExecutor().isVerbose();
// Logging all queries. It is recommended to put any private information that shouldn't be logged into
// the "variables" section of your query. Variable values are not logged.
log.info("Processing GraphQL query:\n{}", queryText);
ExecutionInput.Builder executionInput = new ExecutionInput.Builder().localContext(requestScope).query(queryText);
if (query.getOperationName() != null) {
executionInput.operationName(query.getOperationName());
}
executionInput.variables(variables);
ExecutionResult result = api.execute(executionInput);
tx.preCommit(requestScope);
requestScope.getPermissionExecutor().executeCommitChecks();
if (isMutation) {
if (!result.getErrors().isEmpty()) {
HashMap<String, Object> abortedResponseObject = new HashMap<>();
abortedResponseObject.put("errors", result.getErrors());
abortedResponseObject.put("data", null);
// Do not commit. Throw OK response to process tx.close correctly.
throw new WebApplicationException(Response.ok(mapper.writeValueAsString(abortedResponseObject)).build());
}
requestScope.saveOrCreateObjects();
}
tx.flush(requestScope);
requestScope.runQueuedPreCommitTriggers();
elide.getAuditLogger().commit();
tx.commit(requestScope);
requestScope.runQueuedPostCommitTriggers();
if (log.isTraceEnabled()) {
requestScope.getPermissionExecutor().logCheckStats();
}
return ElideResponse.builder().responseCode(HttpStatus.SC_OK).body(mapper.writeValueAsString(result)).build();
} catch (JsonProcessingException e) {
log.debug("Invalid json body provided to GraphQL", e);
return buildErrorResponse(mapper, new InvalidEntityBodyException(graphQLDocument), isVerbose);
} catch (IOException e) {
log.error("Uncaught IO Exception by Elide in GraphQL", e);
return buildErrorResponse(mapper, new TransactionException(e), isVerbose);
} catch (RuntimeException e) {
return handleRuntimeException(elide, e, isVerbose);
} finally {
elide.getTransactionRegistry().removeRunningTransaction(requestId);
elide.getAuditLogger().clear();
}
}
Aggregations