From fbdd2c6ed1a89d7bb4574ad3b695919005b95df3 Mon Sep 17 00:00:00 2001 From: chrisala Date: Mon, 13 May 2024 10:21:40 +1000 Subject: [PATCH] Ensure project is not cached in session #934 --- .../au/org/ala/ecodata/ProjectService.groovy | 36 +++++++++-------- .../org/ala/ecodata/ProjectServiceSpec.groovy | 40 +++++++++++++++++++ 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/grails-app/services/au/org/ala/ecodata/ProjectService.groovy b/grails-app/services/au/org/ala/ecodata/ProjectService.groovy index 8fab9f617..103cbb4e5 100644 --- a/grails-app/services/au/org/ala/ecodata/ProjectService.groovy +++ b/grails-app/services/au/org/ala/ecodata/ProjectService.groovy @@ -1100,28 +1100,30 @@ class ProjectService { */ Map updateDataSets(String projectId, List dataSets) { synchronized (PROJECT_UPDATE_LOCKS.get(projectId)) { - Project project = Project.findByProjectId(projectId) - if (!project) { - return [status: 'error', error: "No project exists with projectId=${projectId}"] - } - for (Map dataSet in dataSets) { - if (!dataSet.dataSetId) { - dataSet.dataSetId = Identifiers.getNew(true, '') + Project.withNewSession { // Ensure that the queried Project is not cached in the current session which can cause stale data + Project project = Project.findByProjectId(projectId) + if (!project) { + return [status: 'error', error: "No project exists with projectId=${projectId}"] } - Map matchingDataSet = project.custom?.dataSets?.find { it.dataSetId == dataSet.dataSetId } - if (matchingDataSet) { - matchingDataSet.putAll(dataSet) - } else { - if (!project.custom) { - project.custom = [:] + for (Map dataSet in dataSets) { + if (!dataSet.dataSetId) { + dataSet.dataSetId = Identifiers.getNew(true, '') } - if (!project.custom?.dataSets) { - project.custom.dataSets = [] + Map matchingDataSet = project.custom?.dataSets?.find { it.dataSetId == dataSet.dataSetId } + if (matchingDataSet) { + matchingDataSet.putAll(dataSet) + } else { + if (!project.custom) { + project.custom = [:] + } + if (!project.custom?.dataSets) { + project.custom.dataSets = [] + } + project.custom.dataSets.add(dataSet) } - project.custom.dataSets.add(dataSet) } + update([custom: project.custom], project.projectId, false) } - update([custom: project.custom], project.projectId, false) } } diff --git a/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy b/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy index a5b01f8e7..06d9b1e4b 100644 --- a/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy @@ -9,6 +9,10 @@ import org.grails.web.converters.marshaller.json.CollectionMarshaller import org.grails.web.converters.marshaller.json.MapMarshaller import spock.lang.Ignore +import java.util.concurrent.Callable +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest { ProjectActivityService projectActivityServiceStub = Stub(ProjectActivityService) @@ -850,4 +854,40 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest