Search in sources :

Example 1 with JpaOptimisticLockingFailureException

use of org.springframework.orm.jpa.JpaOptimisticLockingFailureException in project open-smart-grid-platform by OSGP.

the class RtuResponseService method handleResponseMessageReceived.

@Transactional(value = "transactionManager")
public void handleResponseMessageReceived(final Logger logger, final String deviceIdentification, final boolean expectDeviceToBeKnown) throws FunctionalException {
    try {
        final RtuDevice device = this.rtuDeviceRepository.findByDeviceIdentification(deviceIdentification).orElse(null);
        if (device == null && expectDeviceToBeKnown) {
            throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, COMPONENT_TYPE, new UnknownEntityException(RtuDevice.class, deviceIdentification));
        } else if (device == null) {
            logger.info("No RTU device {} found to update communication time information for." + " This may be appropriate as the device could be expected to be unknown to GXF.", deviceIdentification);
            return;
        }
        if (this.shouldUpdateCommunicationTime(device, this.minimumDurationBetweenCommunicationTimeUpdates)) {
            device.messageReceived();
            this.rtuDeviceRepository.save(device);
        } else {
            logger.info("Last communication time within duration: {}. Skipping last communication date update.", this.minimumDurationBetweenCommunicationTimeUpdates);
        }
    } catch (final OptimisticLockException | JpaOptimisticLockingFailureException ex) {
        logger.warn("Last communication time not updated due to optimistic lock exception", ex);
    }
}
Also used : RtuDevice(org.opensmartgridplatform.domain.core.entities.RtuDevice) JpaOptimisticLockingFailureException(org.springframework.orm.jpa.JpaOptimisticLockingFailureException) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) OptimisticLockException(javax.persistence.OptimisticLockException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with JpaOptimisticLockingFailureException

use of org.springframework.orm.jpa.JpaOptimisticLockingFailureException in project open-smart-grid-platform by OSGP.

the class ReportingService method storeLastReportEntry.

@Transactional(value = "transactionManager")
public void storeLastReportEntry(final Report report, final String deviceIdentification) {
    if (Objects.isNull(report.getEntryId()) || Objects.isNull(report.getTimeOfEntry())) {
        LOGGER.warn("Not all report entry data availabe for report id {} and device identification {}, skip storing last report entry", report.getRptId(), deviceIdentification);
        return;
    }
    Iec61850ReportEntry reportEntry = this.iec61850ReportEntryRepository.findByDeviceIdentificationAndReportId(deviceIdentification, report.getRptId());
    if (reportEntry == null) {
        reportEntry = new Iec61850ReportEntry(deviceIdentification, report.getRptId(), report.getEntryId().getValue(), new Date(report.getTimeOfEntry().getTimestampValue()));
        LOGGER.info("Store new last report entry: {}", reportEntry);
    } else {
        reportEntry.updateLastReportEntry(report.getEntryId().getValue(), new Date(report.getTimeOfEntry().getTimestampValue()));
        LOGGER.info("Store updated last report entry: {}", reportEntry);
    }
    try {
        this.iec61850ReportEntryRepository.saveAndFlush(reportEntry);
    } catch (final JpaOptimisticLockingFailureException e) {
        LOGGER.debug("JpaOptimisticLockingFailureException", e);
        LOGGER.warn("JPA optimistic locking failure exception while saving last report entry: {} with id {} and version {}", reportEntry, reportEntry.getId(), reportEntry.getVersion());
    }
}
Also used : JpaOptimisticLockingFailureException(org.springframework.orm.jpa.JpaOptimisticLockingFailureException) Iec61850ReportEntry(org.opensmartgridplatform.adapter.protocol.iec61850.domain.entities.Iec61850ReportEntry) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

JpaOptimisticLockingFailureException (org.springframework.orm.jpa.JpaOptimisticLockingFailureException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Date (java.util.Date)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 Iec61850ReportEntry (org.opensmartgridplatform.adapter.protocol.iec61850.domain.entities.Iec61850ReportEntry)1 RtuDevice (org.opensmartgridplatform.domain.core.entities.RtuDevice)1 UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)1 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)1