Search in sources :

Example 11 with ConcurrentEntityModificationException

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();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Reference(org.restlet.data.Reference) Uniform(org.restlet.Uniform) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) ResourceException(org.restlet.resource.ResourceException) Usecase(org.qi4j.api.usecase.Usecase)

Example 12 with ConcurrentEntityModificationException

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("------------------------------------");
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) AbstractRequestCycleListener(org.apache.wicket.request.cycle.AbstractRequestCycleListener)

Example 13 with ConcurrentEntityModificationException

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);
        }
    }
}
Also used : ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Aggregations

ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)13 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)12 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)11 ArrayList (java.util.ArrayList)5 Serializable (java.io.Serializable)4 Test (org.junit.Test)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 Collection (java.util.Collection)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1 EntityBuilder (org.qi4j.api.entity.EntityBuilder)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityReference (org.qi4j.api.entity.EntityReference)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)1 Usecase (org.qi4j.api.usecase.Usecase)1 Function (org.qi4j.functional.Function)1 ServiceDebugRecordEntity (org.qi4j.logging.debug.records.ServiceDebugRecordEntity)1