use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.
the class ContextRestlet method handle.
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
MDC.put("url", request.getResourceRef().toString());
try {
int tries = 0;
// TODO Make this number configurable
while (tries < 10) {
tries++;
// Root of the call
Reference ref = request.getResourceRef();
List<String> segments = ref.getScheme().equals("riap") ? ref.getRelativeRef(new Reference("riap://application/")).getSegments() : ref.getRelativeRef().getSegments();
// Handle conversion of verbs into standard interactions
if (segments.get(segments.size() - 1).equals("")) {
if (request.getMethod().equals(Method.DELETE)) {
// Translate DELETE into command "delete"
segments.set(segments.size() - 1, "delete");
} else if (request.getMethod().equals(Method.PUT)) {
// Translate PUT into command "update"
segments.set(segments.size() - 1, "update");
}
}
request.getAttributes().put("segments", segments);
request.getAttributes().put("template", new StringBuilder("/rest/"));
Usecase usecase = UsecaseBuilder.buildUsecase(getUsecaseName(request)).withMetaInfo(request.getMethod().isSafe() ? CacheOptions.ALWAYS : CacheOptions.NEVER).newUsecase();
UnitOfWork uow = module.newUnitOfWork(usecase);
ObjectSelection.newSelection();
try {
// Start handling the build-up for the context
Uniform resource = createRoot(request, response);
resource.handle(request, response);
if (response.getEntity() != null) {
if (response.getEntity().getModificationDate() == null) {
ResourceValidity validity = (ResourceValidity) Request.getCurrent().getAttributes().get(ContextResource.RESOURCE_VALIDITY);
if (validity != null) {
validity.updateResponse(response);
}
}
// Check if characterset is set
if (response.getEntity().getCharacterSet() == null) {
response.getEntity().setCharacterSet(CharacterSet.UTF_8);
}
// Check if language is set
if (response.getEntity().getLanguages().isEmpty()) {
response.getEntity().getLanguages().add(Language.ENGLISH);
}
uow.discard();
} else {
// Check if last modified and tag is set
ResourceValidity validity = null;
try {
validity = ObjectSelection.type(ResourceValidity.class);
} catch (IllegalArgumentException e) {
// Ignore
}
uow.complete();
Object result = commandResult.getResult();
if (result != null) {
if (result instanceof Representation) {
response.setEntity((Representation) result);
} else {
if (!responseWriter.writeResponse(result, response)) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Could not write result of type " + result.getClass().getName());
}
}
if (response.getEntity() != null) {
// Check if characterset is set
if (response.getEntity().getCharacterSet() == null) {
response.getEntity().setCharacterSet(CharacterSet.UTF_8);
}
// Check if language is set
if (response.getEntity().getLanguages().isEmpty()) {
response.getEntity().getLanguages().add(Language.ENGLISH);
}
// Check if last modified and tag should be set
if (validity != null) {
UnitOfWork lastModifiedUoW = module.newUnitOfWork();
try {
validity.updateEntity(lastModifiedUoW);
validity.updateResponse(response);
} finally {
lastModifiedUoW.discard();
}
}
}
}
return;
}
return;
} catch (ConcurrentEntityModificationException ex) {
uow.discard();
// Try again
ObjectSelection.newSelection();
} catch (Throwable e) {
uow.discard();
handleException(response, e);
return;
}
}
// Try again
} finally {
MDC.clear();
}
}
use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.
the class WicketQi4jApplication method handleUnitOfWork.
private void handleUnitOfWork() {
getRequestCycleListeners().add(new AbstractRequestCycleListener() {
@Override
public void onBeginRequest(final RequestCycle requestCycle) {
super.onBeginRequest(requestCycle);
logger.debug("================================");
logger.debug("REQUEST start");
logger.debug(requestCycle.getRequest().toString());
logger.debug(requestCycle.getRequest().getRequestParameters().toString());
UnitOfWork uow = uowf.newUnitOfWork(UsecaseBuilder.newUsecase("REQUEST"));
logger.debug(" ### NEW " + uow + " ### MODULE: " + qi4jModule);
}
@Override
public void onEndRequest(final RequestCycle requestCycle) {
UnitOfWork uow = uowf.currentUnitOfWork();
if (uow != null) {
try {
if ("POST".equals(((HttpServletRequest) requestCycle.getRequest().getContainerRequest()).getMethod())) {
// "Save"
logger.debug(" ### COMPLETE " + uow + " ### MODULE: " + qi4jModule);
uow.complete();
} else {
// GET requests
logger.debug(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
}
} catch (ConcurrentEntityModificationException e) {
logger.error(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
e.printStackTrace();
} catch (UnitOfWorkCompletionException e) {
logger.error(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
e.printStackTrace();
}
}
logger.debug("REQUEST end");
logger.debug("------------------------------------");
}
});
}
use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.
the class PersistedSequencingMixin method newSequenceValue.
@Override
public Long newSequenceValue() throws SequencingException {
synchronized (this) {
ConcurrentEntityModificationException exc = null;
UnitOfWork uow = uowf.newUnitOfWork();
try {
for (int i = 0; i < 3; i++) {
try {
Property<Long> property = sequence.get().currentValue();
long value = property.get();
value = value + 1;
property.set(value);
uow.complete();
return value;
} catch (ConcurrentEntityModificationException e) {
// Ignore;
exc = e;
}
}
throw new SequencingException("Unable to update sequence value.", exc);
} catch (UnitOfWorkCompletionException e) {
throw new SequencingException("Unable to update sequence value.", exc);
}
}
}
Aggregations