Example usage for javax.persistence LockModeType PESSIMISTIC_FORCE_INCREMENT

List of usage examples for javax.persistence LockModeType PESSIMISTIC_FORCE_INCREMENT

Introduction

In this page you can find the example usage for javax.persistence LockModeType PESSIMISTIC_FORCE_INCREMENT.

Prototype

LockModeType PESSIMISTIC_FORCE_INCREMENT

To view the source code for javax.persistence LockModeType PESSIMISTIC_FORCE_INCREMENT.

Click Source Link

Document

Pessimistic write lock, with version update.

Usage

From source file:com.abiquo.server.core.cloud.VirtualMachineDAO.java

public void refreshLock(final VirtualMachine vm) {
    // We force the refresh from database (PESSIMISTIC) and increment the version of the object
    // (INCREMENT) to ensure that no other code will be using the "unlocked" object.
    getEntityManager().refresh(vm, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
}

From source file:eu.ggnet.dwoss.receipt.UnitProcessorOperation.java

private void executeOperation(UniqueUnit uniqueUnit, StockUnit stockUnit, ReceiptOperation operation,
        String operationComment, String arranger) {
    long customerId = receiptCustomers.getCustomerId(uniqueUnit.getContractor(), operation);
    Document doc = new DossierEmo(redTapeEm).requestActiveDocumentBlock((int) customerId,
            "Blockaddresse KundenId " + customerId, "Erzeugung durch " + operation, arranger);
    L.debug("requestActiveDocumentBlock = {} with dossier = {}", doc, doc.getDossier());
    redTapeEm.flush();//  www. ja v  a  2 s  . c  o  m
    redTapeEm.refresh(doc, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
    L.debug("Refreshed requestActiveDocumentBlock to = {} with dossier = {}", doc, doc.getDossier());
    if (!doc.isActive())
        throw new RuntimeException(
                "The Document(id={}) has changed to inactive while locking, this was very unlikely, inform Administrator");
    doc.append(toPosition(uniqueUnit, operationComment));
    LogicTransaction lt = new LogicTransactionEmo(stockEm).request(doc.getDossier().getId(),
            LockModeType.PESSIMISTIC_FORCE_INCREMENT);
    lt.add(stockUnit); // Implicit removes it from an existing LogicTransaction
    L.debug("Executed Operation {} for uniqueUnit(id={},refurbishId={}), added to LogicTransaction({}) and Dossier({})",
            operation, uniqueUnit.getId(), uniqueUnit.getRefurbishId(), lt.getId(),
            doc.getDossier().getIdentifier());
    uniqueUnit.addHistory(UniqueUnitHistory.Type.STOCK,
            "RecepitOeration:" + operation + ", " + operationComment + " by " + arranger);
}

From source file:eu.ggnet.dwoss.receipt.UnitProcessorOperation.java

private boolean cleanUpOldOperation(UniqueUnit uniqueUnit, StockUnit stockUnit,
        ReceiptOperation updateOperation, String operationComment, String arranger) {
    LogicTransaction oldLogicTransaction = stockUnit.getLogicTransaction();
    if (oldLogicTransaction != null) {
        Dossier oldDossier = new DossierEao(redTapeEm).findById(oldLogicTransaction.getDossierId());
        ReceiptOperation oldOperation = receiptCustomers.getOperation(oldDossier.getCustomerId()).orElse(null);
        Document oldDocument = oldDossier.getActiveDocuments().get(0);
        redTapeEm.flush();/*  w  w  w . jav  a  2s  .c  om*/
        redTapeEm.refresh(oldDocument, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        if (!oldDocument.isActive())
            throw new RuntimeException(
                    "The Document(id={}) has changed to inactive while locking, this was very unlikely, inform Administrator");
        Position oldPosition = oldDocument.getPositionByUniqueUnitId(uniqueUnit.getId());
        if (oldOperation == updateOperation) {
            oldPosition.setDescription(oldPosition.getDescription() + ", Aufnahme: " + operationComment);
            L.debug("old operation and update operation are {}, nothing more to do", updateOperation);
            return false;
        }
        // cleanUp old Block and Auftrag
        convertToComment(oldPosition, updateOperation);
        L.debug("Old Operation cleanup, removed uniqueUnit(id={},refurbishId={}) from Dossier({})",
                new Object[] { uniqueUnit.getId(), uniqueUnit.getRefurbishId(), oldDossier.getIdentifier() });
    }
    if (updateOperation == ReceiptOperation.SALEABLE) {
        if (oldLogicTransaction != null)
            oldLogicTransaction.remove(stockUnit);
        uniqueUnit.addHistory("Released for Sale by " + arranger);
        L.debug("update operation is {}, nothing more to do", updateOperation);
        return false;
    }
    return true;
}

From source file:ch.puzzle.itc.mobiliar.business.deploy.boundary.DeploymentService.java

/**
 * We update the information about a deployment that has been executed.
 *
 * @param generationModus  - if the deployment was in simulation or realistic mode
 * @param deploymentId     - the deployment id of the deployment that has been executed.
 * @param errorMessage     - the error message if any other
 * @param resourceId       - the ApplicationServe used for deployment
 * @param generationResult/*from w ww .j a  va 2  s.c  om*/
 */
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public DeploymentEntity updateDeploymentInfo(GenerationModus generationModus, final Integer deploymentId,
        final String errorMessage, final Integer resourceId, final GenerationResult generationResult) {
    // don't lock a deployment for predeployment as there is no need to update the deployment.
    if (GenerationModus.PREDEPLOY.equals(generationModus) && errorMessage == null) {
        log.fine("Predeploy script finished at " + new Date());
        return em.find(DeploymentEntity.class, deploymentId);
    }
    DeploymentEntity deployment = em.find(DeploymentEntity.class, deploymentId,
            LockModeType.PESSIMISTIC_FORCE_INCREMENT);

    // set as used for deployment
    if (resourceId != null) {
        ResourceEntity as = em.find(ResourceEntity.class, resourceId);
        deployment.setResource(as);
    }

    if (GenerationModus.DEPLOY.equals(generationModus)) {
        if (errorMessage == null) {
            String nodeInfo = getNodeInfoForDeployment(generationResult);
            deployment.appendStateMessage("Successfully deployed at " + new Date() + "\n" + nodeInfo);
            deployment.setDeploymentState(DeploymentState.success);
        } else {
            deployment.appendStateMessage(errorMessage);
            deployment.setDeploymentState(DeploymentState.failed);
        }
    } else if (GenerationModus.PREDEPLOY.equals(generationModus)) {
        deployment.appendStateMessage(errorMessage);
        deployment.setDeploymentState(DeploymentState.failed);
    } else {
        if (errorMessage == null) {
            String nodeInfo = getNodeInfoForDeployment(generationResult);
            deployment.appendStateMessage("Successfully generated at " + new Date() + "\n" + nodeInfo);
            deployment.setBuildSuccess(true);
        } else {
            deployment.appendStateMessage(errorMessage);
            deployment.setBuildSuccess(false);
        }
        if (deployment.getDeploymentConfirmed() != null && deployment.getDeploymentConfirmed()) {
            deployment.setDeploymentState(DeploymentState.scheduled);
        } else {
            deployment.setDeploymentState(DeploymentState.requested);
        }
    }
    deployment.setSimulating(false);
    return deployment;
}

From source file:ch.puzzle.itc.mobiliar.business.deploy.boundary.DeploymentBoundary.java

/**
 * We update the information about a deployment that has been executed.
 *
 * @param generationModus  - if the deployment was in simulation or realistic mode
 * @param deploymentId     - the deployment id of the deployment that has been executed.
 * @param errorMessage     - the error message if any other
 * @param resourceId       - the ApplicationServe used for deployment
 * @param generationResult/* ww w .  j  a  v a 2s.  c o m*/
 * @param reason           - the DeploymentFailureReason (if any)
 */
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public DeploymentEntity updateDeploymentInfo(GenerationModus generationModus, final Integer deploymentId,
        final String errorMessage, final Integer resourceId, final GenerationResult generationResult,
        DeploymentFailureReason reason) {
    // don't lock a deployment for predeployment as there is no need to update the deployment.
    if (GenerationModus.PREDEPLOY.equals(generationModus) && errorMessage == null) {
        log.fine("Predeploy script finished at " + new Date());
        return em.find(DeploymentEntity.class, deploymentId);
    }
    DeploymentEntity deployment = em.find(DeploymentEntity.class, deploymentId,
            LockModeType.PESSIMISTIC_FORCE_INCREMENT);

    // set as used for deployment
    if (resourceId != null) {
        ResourceEntity as = em.find(ResourceEntity.class, resourceId);
        deployment.setResource(as);
    }

    if (GenerationModus.DEPLOY.equals(generationModus)) {
        if (errorMessage == null) {
            String nodeInfo = getNodeInfoForDeployment(generationResult);
            deployment.appendStateMessage("Successfully deployed at " + new Date() + "\n" + nodeInfo);
            deployment.setDeploymentState(DeploymentState.success);
        } else {
            deployment.appendStateMessage(errorMessage);
            deployment.setDeploymentState(DeploymentState.failed);
            if (reason == null) {
                reason = DeploymentFailureReason.DEPLOYMENT_GENERATION;
            }
            deployment.setReason(reason);
        }
    } else if (GenerationModus.PREDEPLOY.equals(generationModus)) {
        deployment.appendStateMessage(errorMessage);
        deployment.setDeploymentState(DeploymentState.failed);
        if (reason == null) {
            reason = DeploymentFailureReason.PRE_DEPLOYMENT_GENERATION;
        }
        deployment.setReason(reason);
    } else {
        if (errorMessage == null) {
            String nodeInfo = getNodeInfoForDeployment(generationResult);
            deployment.appendStateMessage("Successfully generated at " + new Date() + "\n" + nodeInfo);
            deployment.setBuildSuccess(true);
        } else {
            deployment.appendStateMessage(errorMessage);
            deployment.setBuildSuccess(false);
        }
        if (deployment.getDeploymentConfirmed() != null && deployment.getDeploymentConfirmed()) {
            deployment.setDeploymentState(DeploymentState.scheduled);
        } else {
            deployment.setDeploymentState(DeploymentState.requested);
        }
    }
    deployment.setSimulating(false);
    return deployment;
}

From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java

private List<X> getResultListImpl() {
    this.em.getSession().setLoadTracker();

    final Connection connection = this.em.getConnection();
    try {/*www  . j  a v  a 2  s .c o m*/
        final LockModeType lockMode = this.getLockMode();
        final boolean hasLock = (lockMode == LockModeType.PESSIMISTIC_READ)
                || (lockMode == LockModeType.PESSIMISTIC_WRITE)
                || (lockMode == LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        if (hasLock) {
            this.sql = this.em.getJdbcAdaptor().applyLock(this.sql, lockMode);
        }

        final Object[] parameters = this.applyParameters(connection);

        return this.buildResultSet(connection, parameters);
    } finally {
        this.em.getSession().releaseLoadTracker();

        this.em.closeConnectionIfNecessary();
    }
}