From 510d0fbdc4cbc9dc89b601bb33d4a0d3cc0d94b9 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 3 Dec 2012 07:40:48 +0000
Subject: [PATCH] SP-406, BIS-279: Locks added

SVN: 27822
---
 .../generic/server/dataaccess/db/AbstractDAO.java        | 9 +++++++++
 .../openbis/generic/server/dataaccess/db/DataDAO.java    | 9 +++++++++
 .../generic/server/dataaccess/db/ExperimentDAO.java      | 2 ++
 .../openbis/generic/server/dataaccess/db/SampleDAO.java  | 6 ++++--
 4 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAO.java
index a35d259ab34..cad507f601b 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAO.java
@@ -34,6 +34,7 @@ import javax.validation.Validator;
 import javax.validation.ValidatorFactory;
 
 import org.hibernate.HibernateException;
+import org.hibernate.LockMode;
 import org.hibernate.Query;
 import org.hibernate.SQLQuery;
 import org.hibernate.Session;
@@ -352,6 +353,14 @@ public abstract class AbstractDAO extends HibernateDaoSupport
             });
     }
 
+    protected void lockEntity(Object entityOrNull)
+    {
+        if (entityOrNull != null)
+        {
+            getHibernateTemplate().lock(entityOrNull, LockMode.PESSIMISTIC_WRITE);
+        }
+    }
+
     /**
      * Callback interface for Hibernate code requiring a {@link org.hibernate.StatelessSession}. To
      * be used with {@link HibernateTemplate}'s execution methods, often as anonymous classes within
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DataDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DataDAO.java
index 0437ed475d3..97653da7852 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DataDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DataDAO.java
@@ -639,6 +639,7 @@ final class DataDAO extends AbstractGenericEntityWithPropertiesDAO<DataPE> imple
         }
 
         final HibernateTemplate template = getHibernateTemplate();
+        lockRelatedEntities(dataset);
         template.save(dataset);
         template.flush();
         scheduleDynamicPropertiesEvaluation(Collections.singletonList(dataset));
@@ -649,6 +650,13 @@ final class DataDAO extends AbstractGenericEntityWithPropertiesDAO<DataPE> imple
         }
     }
 
+    private void lockRelatedEntities(DataPE data)
+    {
+        lockEntity(data.getExperiment());
+        lockEntity(data.tryGetSample());
+        lockEntity(data.getContainer());
+    }
+
     @Override
     public void updateDataSet(DataPE data, PersonPE modifier)
     {
@@ -691,6 +699,7 @@ final class DataDAO extends AbstractGenericEntityWithPropertiesDAO<DataPE> imple
             }
             hibernateTemplate.evict(loaded);
         }
+        lockRelatedEntities(data);
         hibernateTemplate.update(data);
         hibernateTemplate.flush();
         scheduleDynamicPropertiesEvaluation(Collections.singletonList(data));
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ExperimentDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ExperimentDAO.java
index 16626f5983c..bfc561e7499 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ExperimentDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ExperimentDAO.java
@@ -387,6 +387,8 @@ public class ExperimentDAO extends AbstractGenericEntityWithPropertiesDAO<Experi
     public void createOrUpdateExperiment(ExperimentPE experiment, PersonPE modifier)
     {
         HibernateTemplate template = getHibernateTemplate();
+        lockEntity(experiment.getProject());
+
         internalCreateOrUpdateExperiment(experiment, modifier, template);
         template.flush();
 
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java
index 01c9cfcaca9..dc5b51fe2ec 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java
@@ -86,8 +86,10 @@ public class SampleDAO extends AbstractGenericEntityWithPropertiesDAO<SamplePE>
         {
             sample.setModificationDate(new Date());
         }
-        // sample.setModifier(modifier);
-        // sample.setModificationDate(new Date());
+        lockEntity(sample.getDatabaseInstance());
+        lockEntity(sample.getSpace());
+        lockEntity(sample.getExperiment());
+        lockEntity(sample.getContainer());
         hibernateTemplate.saveOrUpdate(sample);
         if (doLog && operationLog.isInfoEnabled())
         {
-- 
GitLab