Search in sources :

Example 1 with ConcurrencyFailureException

use of org.springframework.dao.ConcurrencyFailureException in project records-management by Alfresco.

the class RecordsEntityResource method fileRecord.

@Operation("file")
@WebApiDescription(title = "File record", description = "File a record into fileplan.")
public Record fileRecord(String recordId, TargetContainer target, Parameters parameters, WithResponse withResponse) {
    checkNotBlank("recordId", recordId);
    mandatory("target", target);
    mandatory("targetParentId", target.getTargetParentId());
    mandatory("parameters", parameters);
    // Get record and target folder
    NodeRef record = apiUtils.validateRecord(recordId);
    NodeRef targetRecordFolder = apiUtils.lookupAndValidateNodeType(target.getTargetParentId(), RecordsManagementModel.TYPE_RECORD_FOLDER);
    // Get the current parent type to decide if we link or move the record
    NodeRef primaryParent = nodeService.getPrimaryParent(record).getParentRef();
    if (RecordsManagementModel.TYPE_RECORD_FOLDER.equals(nodeService.getType(primaryParent))) {
        recordService.link(record, targetRecordFolder);
    } else {
        try {
            fileFolderService.moveFrom(record, primaryParent, targetRecordFolder, null);
        } catch (FileExistsException e) {
            throw new IntegrityException(e.getMessage(), null);
        } catch (FileNotFoundException e) {
            throw new ConcurrencyFailureException("The record was deleted while filing it", e);
        }
    }
    // return record state
    FileInfo info = fileFolderService.getFileInfo(record);
    return nodesModelFactory.createRecord(info, parameters, null, false);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) WebApiDescription(org.alfresco.rest.framework.WebApiDescription) Operation(org.alfresco.rest.framework.Operation)

Example 2 with ConcurrencyFailureException

use of org.springframework.dao.ConcurrencyFailureException in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method updatePointValueImpl.

long updatePointValueImpl(final int pointId, final PointValueTime pvt, final SetPointSource source, boolean async) {
    DataValue value = pvt.getValue();
    final int dataType = DataTypes.getDataType(value);
    double dvalue = 0;
    String svalue = null;
    if (dataType == DataTypes.IMAGE) {
        ImageValue imageValue = (ImageValue) value;
        dvalue = imageValue.getType();
        if (imageValue.isSaved())
            svalue = Long.toString(imageValue.getId());
    } else if (value.hasDoubleRepresentation())
        dvalue = value.getDoubleValue();
    else
        svalue = value.getStringValue();
    // Check if we need to create an annotation.
    long id;
    try {
        if (svalue != null || source != null || dataType == DataTypes.IMAGE)
            async = false;
        id = updatePointValue(pointId, dataType, dvalue, pvt.getTime(), svalue, source, async);
    } catch (ConcurrencyFailureException e) {
        // Still failed to insert after all of the retries. Store the data
        synchronized (UNSAVED_POINT_UPDATES) {
            UNSAVED_POINT_UPDATES.add(new UnsavedPointUpdate(pointId, pvt, source));
        }
        return -1;
    }
    // Check if we need to save an image
    if (dataType == DataTypes.IMAGE) {
        ImageValue imageValue = (ImageValue) value;
        if (!imageValue.isSaved()) {
            imageValue.setId(id);
            File file = new File(Common.getFiledataPath(), imageValue.getFilename());
            // Write the file.
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);
                StreamUtils.transfer(new ByteArrayInputStream(imageValue.getData()), out);
            } catch (IOException e) {
                // Rethrow as an RTE
                throw new ImageSaveException(e);
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (IOException e) {
                // no op
                }
            }
            // Allow the data to be GC'ed
            imageValue.setData(null);
        }
    }
    clearUnsavedPointUpdates();
    return id;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) FileOutputStream(java.io.FileOutputStream) ImageSaveException(com.serotonin.m2m2.ImageSaveException) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) File(java.io.File)

Example 3 with ConcurrencyFailureException

use of org.springframework.dao.ConcurrencyFailureException in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method savePointValueSync.

/**
 * Only the PointValueCache should call this method during runtime. Do not use.
 */
@Override
public PointValueTime savePointValueSync(int pointId, PointValueTime pointValue, SetPointSource source) {
    long id = savePointValueImpl(pointId, pointValue, source, false);
    PointValueTime savedPointValue;
    int retries = 5;
    while (true) {
        try {
            savedPointValue = getPointValue(id);
            break;
        } catch (ConcurrencyFailureException e) {
            if (retries <= 0)
                throw e;
            retries--;
        }
    }
    return savedPointValue;
}
Also used : ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)

Example 4 with ConcurrencyFailureException

use of org.springframework.dao.ConcurrencyFailureException in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method updatePointValueSync.

/**
 * Only the PointValueCache should call this method during runtime. Do not use.
 */
@Override
public PointValueTime updatePointValueSync(int dataPointId, PointValueTime pvt, SetPointSource source) {
    long id = updatePointValueImpl(dataPointId, pvt, source, false);
    PointValueTime savedPointValue;
    int retries = 5;
    while (true) {
        try {
            savedPointValue = getPointValue(id);
            break;
        } catch (ConcurrencyFailureException e) {
            if (retries <= 0)
                throw e;
            retries--;
        }
    }
    return savedPointValue;
}
Also used : ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedIdPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)

Example 5 with ConcurrencyFailureException

use of org.springframework.dao.ConcurrencyFailureException in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method testTransactionWithDataAccessExceptionOnRollback.

@Test
public void testTransactionWithDataAccessExceptionOnRollback() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    willThrow(new SQLException("Cannot rollback")).given(con).rollback();
    tm.setExceptionTranslator((task, sql, ex) -> new ConcurrencyFailureException(task));
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            status.setRollbackOnly();
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).rollback();
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ConcurrencyFailureException (org.springframework.dao.ConcurrencyFailureException)10 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 FileExistsException (org.alfresco.service.cmr.model.FileExistsException)3 FileInfo (org.alfresco.service.cmr.model.FileInfo)3 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 Test (org.junit.jupiter.api.Test)3 ImageSaveException (com.serotonin.m2m2.ImageSaveException)2 AnnotatedIdPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedIdPointValueTime)2 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)2 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)2 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)2 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)2