diff --git a/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java b/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java index 6d239d7..e4acc26 100644 --- a/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java +++ b/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java @@ -62,20 +62,22 @@ public class CsvParserService implements DataParser { @PostConstruct @Transactional public void init() { - long vesselData = vesselDataRepository.count(); - log.info("Found {} vessel data in vessel_data db ", vesselData); - boolean loadData = vesselData <= 0 || loadCsvIfAlreadyHaveData; - if (loadData) { - log.info("CsvParserService will load data from {} file", vesselDataPath); - try (InputStream inputStream = getClass().getResourceAsStream(vesselDataPath)) { - parseAndSave(inputStream); - checkForOutliers(); - } catch (Exception e) { - log.error("Error initializing data: ", e); - throw new RuntimeException("Failed to initialize data", e); + synchronized (this) { + long vesselData = vesselDataRepository.count(); + log.info("Found {} vessel data in vessel_data db ", vesselData); + boolean loadData = vesselData <= 0 || loadCsvIfAlreadyHaveData; + if (loadData) { + log.info("CsvParserService will load data from {} file", vesselDataPath); + try (InputStream inputStream = getClass().getResourceAsStream(vesselDataPath)) { + parseAndSave(inputStream); + checkForOutliers(); + } catch (Exception e) { + log.error("Error initializing data: ", e); + throw new RuntimeException("Failed to initialize data", e); + } + } else { + log.info("Skipping loading again vessel data from CSV file"); } - } else { - log.info("Skipping loading again vessel data from CSV file"); } }