From 1fd9ef496dc93d6f638ea29d5371092a18e8a3c6 Mon Sep 17 00:00:00 2001
From: anttil <anttil>
Date: Thu, 7 Feb 2013 10:23:16 +0000
Subject: [PATCH] BIS-316 / SP-480: Add property to control limit of entities
 stored in Hibernate session cache during batch processing.

SVN: 28312
---
 .../server/batch/SampleBatchRegistration.java | 11 ++++++-
 .../server/batch/SampleBatchUpdate.java       |  8 +++--
 .../GenericSampleTypeSlaveServerPlugin.java   | 32 +++++++++++++++++--
 openbis/source/java/service.properties        |  3 ++
 4 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchRegistration.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchRegistration.java
index 2467ba5f32d..e1f73d83a3c 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchRegistration.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchRegistration.java
@@ -39,19 +39,28 @@ public class SampleBatchRegistration implements IBatchOperation<NewSample>, IPro
 
     private int maxIndex;
 
+    private int limit;
+
     public SampleBatchRegistration(ISampleTable businessTable, List<NewSample> entities,
             PersonPE registratorOrNull)
+    {
+        this(businessTable, entities, registratorOrNull, 50000);
+    }
+
+    public SampleBatchRegistration(ISampleTable businessTable, List<NewSample> entities,
+            PersonPE registratorOrNull, int limit)
     {
         this.businessTable = businessTable;
         this.entities = entities;
         this.registratorOrNull = registratorOrNull;
+        this.limit = limit;
     }
 
     @Override
     public void execute(List<NewSample> batch)
     {
         businessTable.prepareForRegistration(batch, registratorOrNull);
-        businessTable.save(maxIndex - endIndex > 50000);
+        businessTable.save(maxIndex - endIndex > limit);
     }
 
     @Override
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchUpdate.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchUpdate.java
index 5d068dd793c..504e46ae5aa 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchUpdate.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/batch/SampleBatchUpdate.java
@@ -36,17 +36,21 @@ public class SampleBatchUpdate implements IBatchOperation<SampleBatchUpdatesDTO>
 
     private int maxIndex;
 
-    public SampleBatchUpdate(ISampleTable businessTable, List<SampleBatchUpdatesDTO> entities)
+    int limit;
+
+    public SampleBatchUpdate(ISampleTable businessTable, List<SampleBatchUpdatesDTO> entities,
+            int limit)
     {
         this.businessTable = businessTable;
         this.entities = entities;
+        this.limit = limit;
     }
 
     @Override
     public void execute(List<SampleBatchUpdatesDTO> updates)
     {
         businessTable.prepareForUpdate(updates);
-        businessTable.save(maxIndex - endIndex > 50000);
+        businessTable.save(maxIndex - endIndex > limit);
     }
 
     @Override
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericSampleTypeSlaveServerPlugin.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericSampleTypeSlaveServerPlugin.java
index 04e75110bd8..ad0e2cc9ae9 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericSampleTypeSlaveServerPlugin.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericSampleTypeSlaveServerPlugin.java
@@ -17,12 +17,15 @@
 package ch.systemsx.cisd.openbis.plugin.generic.server;
 
 import java.util.List;
+import java.util.Properties;
 
+import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 
 import org.springframework.stereotype.Component;
 
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
+import ch.systemsx.cisd.common.spring.ExposablePropertyPlaceholderConfigurer;
 import ch.systemsx.cisd.openbis.generic.server.ComponentNames;
 import ch.systemsx.cisd.openbis.generic.server.batch.BatchOperationExecutor;
 import ch.systemsx.cisd.openbis.generic.server.batch.SampleBatchRegistration;
@@ -53,10 +56,31 @@ public final class GenericSampleTypeSlaveServerPlugin implements ISampleTypeSlav
     @Resource(name = ComponentNames.DAO_FACTORY)
     private IDAOFactory daoFactory;
 
+    @Resource(name = ExposablePropertyPlaceholderConfigurer.PROPERTY_CONFIGURER_BEAN_NAME)
+    protected ExposablePropertyPlaceholderConfigurer configurer;
+
+    private int sessionCacheEntityLimit = 50000;
+
     private GenericSampleTypeSlaveServerPlugin()
     {
     }
 
+    @PostConstruct
+    public void init()
+    {
+        Properties props = this.configurer.getResolvedProps();
+        String text = props.getProperty("hibernate.batch.sessionCache.maxEntities");
+        if (text != null)
+        {
+            try
+            {
+                sessionCacheEntityLimit = Integer.parseInt(text);
+            } catch (NumberFormatException e)
+            {
+            }
+        }
+    }
+
     //
     // ISlaveServerPlugin
     //
@@ -82,8 +106,10 @@ public final class GenericSampleTypeSlaveServerPlugin implements ISampleTypeSlav
         assert session != null : "Unspecified session.";
         assert newSamples != null && newSamples.size() > 0 : "Unspecified sample or empty samples.";
 
-        BatchOperationExecutor.executeInBatches(new SampleBatchRegistration(businessObjectFactory
-                .createSampleTable(session), newSamples, registratorOrNull));
+        BatchOperationExecutor
+                .executeInBatches(new SampleBatchRegistration(businessObjectFactory
+                        .createSampleTable(session), newSamples, registratorOrNull,
+                        sessionCacheEntityLimit));
     }
 
     @Override
@@ -93,6 +119,6 @@ public final class GenericSampleTypeSlaveServerPlugin implements ISampleTypeSlav
         assert updateSamples != null && updateSamples.size() > 0 : "Unspecified sample or empty samples.";
 
         BatchOperationExecutor.executeInBatches(new SampleBatchUpdate(businessObjectFactory
-                .createSampleTable(session), updateSamples));
+                .createSampleTable(session), updateSamples, sessionCacheEntityLimit));
     }
 }
diff --git a/openbis/source/java/service.properties b/openbis/source/java/service.properties
index f4cf1a68739..8301ef08f57 100644
--- a/openbis/source/java/service.properties
+++ b/openbis/source/java/service.properties
@@ -87,6 +87,9 @@ hibernate.search.batch-size = 1000
 hibernate.search.maxResults = 100000
 # If 'async', the update of indices will be done in a separate thread.
 hibernate.search.worker.execution=async
+# Limit of entities stored in hibernate session cache during batch processing. Default 50000.
+# Note - this is our own setting, not Hibernate's.
+#hibernate.batch.sessionCache.maxEntities = 50000
 
 # Online Help
 #
-- 
GitLab