From d4b1fe2a1ba455bd5fb8ba8ea1bf87b1f9a1fb0c Mon Sep 17 00:00:00 2001 From: Frank Mittag Date: Tue, 13 Mar 2018 11:50:10 +0100 Subject: [PATCH] added synchronization for the case that the same competitor could be migrated twice triggered by two different tracked races from the same or even different regattas (in case of league events) --- .../impl/TransientCompetitorStoreImpl.java | 5 +- .../impl/DomainFactoryImpl.java | 58 ++++++++++--------- .../impl/PersistentCompetitorStore.java | 5 +- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/java/com.sap.sailing.domain.shared.android/src/com/sap/sailing/domain/base/impl/TransientCompetitorStoreImpl.java b/java/com.sap.sailing.domain.shared.android/src/com/sap/sailing/domain/base/impl/TransientCompetitorStoreImpl.java index 33f15245b6b..64461ce08cc 100755 --- a/java/com.sap.sailing.domain.shared.android/src/com/sap/sailing/domain/base/impl/TransientCompetitorStoreImpl.java +++ b/java/com.sap.sailing.domain.shared.android/src/com/sap/sailing/domain/base/impl/TransientCompetitorStoreImpl.java @@ -113,7 +113,10 @@ public class TransientCompetitorStoreImpl implements CompetitorStore, Serializab } @Override - public Competitor migrateToCompetitorWithoutBoat(CompetitorWithBoat competitorWithBoat) { + public synchronized Competitor migrateToCompetitorWithoutBoat(CompetitorWithBoat competitorWithBoat) { + // It should not be possible to call this for a competitor which has already been migrated. + assert competitorWithBoat.hasBoat(); + // We can't create a new competitor without boat here as the existing competitorWithBoat might be already referenced. // Therefore we only clear the 'boat' property and make sure that removeBoat(competitorWithBoat.getBoat()); diff --git a/java/com.sap.sailing.domain.tractracadapter/src/com/sap/sailing/domain/tractracadapter/impl/DomainFactoryImpl.java b/java/com.sap.sailing.domain.tractracadapter/src/com/sap/sailing/domain/tractracadapter/impl/DomainFactoryImpl.java index 4039a2c3f76..9d11afd1872 100755 --- a/java/com.sap.sailing.domain.tractracadapter/src/com/sap/sailing/domain/tractracadapter/impl/DomainFactoryImpl.java +++ b/java/com.sap.sailing.domain.tractracadapter/src/com/sap/sailing/domain/tractracadapter/impl/DomainFactoryImpl.java @@ -741,39 +741,41 @@ public class DomainFactoryImpl implements DomainFactory { } Boat existingBoat = competitorAndBoatStore.getExistingBoatById(boatId); Competitor existingCompetitor = competitorAndBoatStore.getExistingCompetitorById(competitorId); - boolean competitorMigrationRequired = existingCompetitor != null && existingCompetitor.hasBoat(); - // Now we check if we already have a separate competitor and boat for this competitor in the store - if (existingCompetitor != null && existingBoat != null && !competitorMigrationRequired) { - competitorsAndBoats.put(existingCompetitor, existingBoat); - } else { - Competitor competitorToUse = existingCompetitor; - if (existingCompetitor == null) { - competitorToUse = getOrCreateCompetitor(rc.getCompetitor()); - } else if (competitorMigrationRequired) { - competitorToUse = competitorAndBoatStore.migrateToCompetitorWithoutBoat((CompetitorWithBoat) existingCompetitor); - // we might also want to update the shortName field of the competitor during migration (instead of using sailID) - if (competitorToUse.getShortName() != rc.getCompetitor().getShortName()) { - boolean savedIsCompetitorToUpdateDuringGetOrCreate = competitorAndBoatStore.isCompetitorToUpdateDuringGetOrCreate(competitorToUse); - competitorAndBoatStore.allowCompetitorResetToDefaults(competitorToUse); - competitorAndBoatStore.updateCompetitor(competitorToUse.getId().toString(), competitorToUse.getName(), - rc.getCompetitor().getShortName(), competitorToUse.getColor(), - competitorToUse.getEmail(), competitorToUse.getNationality(), competitorToUse.getTeam().getImage(), - competitorToUse.getFlagImage(), competitorToUse.getTimeOnTimeFactor(), - competitorToUse.getTimeOnDistanceAllowancePerNauticalMile(), competitorToUse.getSearchTag()); - if (savedIsCompetitorToUpdateDuringGetOrCreate) { + synchronized (existingCompetitor) { + boolean competitorMigrationRequired = existingCompetitor != null && existingCompetitor.hasBoat(); + // Now we check if we already have a separate competitor and boat for this competitor in the store + if (existingCompetitor != null && existingBoat != null && !competitorMigrationRequired) { + competitorsAndBoats.put(existingCompetitor, existingBoat); + } else { + Competitor competitorToUse = existingCompetitor; + if (existingCompetitor == null) { + competitorToUse = getOrCreateCompetitor(rc.getCompetitor()); + } else if (competitorMigrationRequired) { + competitorToUse = competitorAndBoatStore.migrateToCompetitorWithoutBoat((CompetitorWithBoat) existingCompetitor); + // we might also want to update the shortName field of the competitor during migration (instead of using sailID) + if (competitorToUse.getShortName() != rc.getCompetitor().getShortName()) { + boolean savedIsCompetitorToUpdateDuringGetOrCreate = competitorAndBoatStore.isCompetitorToUpdateDuringGetOrCreate(competitorToUse); competitorAndBoatStore.allowCompetitorResetToDefaults(competitorToUse); + competitorAndBoatStore.updateCompetitor(competitorToUse.getId().toString(), competitorToUse.getName(), + rc.getCompetitor().getShortName(), competitorToUse.getColor(), + competitorToUse.getEmail(), competitorToUse.getNationality(), competitorToUse.getTeam().getImage(), + competitorToUse.getFlagImage(), competitorToUse.getTimeOnTimeFactor(), + competitorToUse.getTimeOnDistanceAllowancePerNauticalMile(), competitorToUse.getSearchTag()); + if (savedIsCompetitorToUpdateDuringGetOrCreate) { + competitorAndBoatStore.allowCompetitorResetToDefaults(competitorToUse); + } } } - } - Boat boatToUse = existingBoat; - if (existingBoat == null) { - if (competitorBoatInfo != null) { - boatToUse = getOrCreateBoat(boatId, competitorBoatInfo.getName(), defaultBoatClass, sailId, AbstractColor.getCssColor(competitorBoatInfo.getColor())); - } else { - boatToUse = getOrCreateBoat(boatId, /* boat name*/ null, defaultBoatClass, sailId, null); + Boat boatToUse = existingBoat; + if (existingBoat == null) { + if (competitorBoatInfo != null) { + boatToUse = getOrCreateBoat(boatId, competitorBoatInfo.getName(), defaultBoatClass, sailId, AbstractColor.getCssColor(competitorBoatInfo.getColor())); + } else { + boatToUse = getOrCreateBoat(boatId, /* boat name*/ null, defaultBoatClass, sailId, null); + } } + competitorsAndBoats.put(competitorToUse, boatToUse); } - competitorsAndBoats.put(competitorToUse, boatToUse); } } else { // Case 2 we assume here that the boat is contained in competitor as it's always the same diff --git a/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/PersistentCompetitorStore.java b/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/PersistentCompetitorStore.java index b9dac049d0b..b3dc8d4db77 100755 --- a/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/PersistentCompetitorStore.java +++ b/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/PersistentCompetitorStore.java @@ -121,7 +121,10 @@ public class PersistentCompetitorStore extends TransientCompetitorStoreImpl impl } @Override - public Competitor migrateToCompetitorWithoutBoat(CompetitorWithBoat competitorWithBoat) { + public synchronized Competitor migrateToCompetitorWithoutBoat(CompetitorWithBoat competitorWithBoat) { + // It should not be possible to call this for a competitor which has already been migrated. + assert competitorWithBoat.hasBoat(); + logger.log(Level.INFO, "Bug2822 DB-Migration: Migrate competitor " + competitorWithBoat.getName() + " with ID " + competitorWithBoat.getId().toString() + " to have a separate boat"); Competitor migratedCompetitor = super.migrateToCompetitorWithoutBoat(competitorWithBoat); storeTo.storeCompetitor(migratedCompetitor);