From 4b7d77765e94031da4bc2885ce27dc3075f0a39e Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Fri, 22 Oct 2010 07:39:45 +0000
Subject: [PATCH] [LMS-1846] moved setting batch update mode to HibernateUtils

SVN: 18411
---
 .../db/AuthorizationDAOFactory.java           | 19 +++++------------
 .../generic/shared/util/HibernateUtils.java   | 21 +++++++++++++++++++
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java
index b0a1ad0ef2c..8ae73c48d67 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java
@@ -18,8 +18,6 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.db;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
-import org.hibernate.CacheMode;
-import org.hibernate.FlushMode;
 import org.hibernate.SessionFactory;
 import org.hibernate.classic.Session;
 import org.springframework.dao.DataAccessException;
@@ -46,6 +44,7 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.PersistencyResources;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.dynamic_property.IDynamicPropertyEvaluationScheduler;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.IFullTextIndexUpdateScheduler;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE;
+import ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils;
 import ch.systemsx.cisd.openbis.generic.shared.util.UuidUtil;
 
 /**
@@ -248,23 +247,15 @@ public class AuthorizationDAOFactory implements IAuthorizationDAOFactory
     }
 
     /**
-     * @param batchMode if true the second level cache will be switched off and the hibernate
-     *            session will be synchronized with the database only at the end of the transaction.
-     *            Note that 1) this cause that the stale data will be fetched from database,
-     *            ignoring the changes in hibernate layer 2) if you have many write operations
-     *            interleaved with read operations in one block, switching synchronization off
-     *            greatly improves the performance.
+     * Configures current session settings for batch update mode.
+     * 
+     * @see HibernateUtils#setBatchUpdateMode(org.hibernate.Session, boolean)
      */
     public void setBatchUpdateMode(boolean batchMode)
     {
         SessionFactory sessionFactory = persistencyResources.getSessionFactoryOrNull();
         Session currentSession = sessionFactory.getCurrentSession();
-
-        CacheMode cacheMode = (batchMode ? CacheMode.IGNORE : CacheMode.NORMAL);
-        currentSession.setCacheMode(cacheMode);
-
-        FlushMode mode = (batchMode ? FlushMode.COMMIT : FlushMode.AUTO);
-        currentSession.setFlushMode(mode);
+        HibernateUtils.setBatchUpdateMode(currentSession, batchMode);
     }
 
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/HibernateUtils.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/HibernateUtils.java
index cacc7513a0f..791606e94d6 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/HibernateUtils.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/HibernateUtils.java
@@ -16,7 +16,10 @@
 
 package ch.systemsx.cisd.openbis.generic.shared.util;
 
+import org.hibernate.CacheMode;
+import org.hibernate.FlushMode;
 import org.hibernate.Hibernate;
+import org.hibernate.Session;
 import org.hibernate.engine.SessionImplementor;
 import org.hibernate.proxy.HibernateProxy;
 import org.hibernate.proxy.LazyInitializer;
@@ -113,4 +116,22 @@ public final class HibernateUtils
             return proxy;
         }
     }
+
+    /**
+     * @param batchMode if true the second level cache will be switched off and the hibernate
+     *            session will be synchronized with the database only at the end of the transaction.
+     *            Note that 1) this cause that the stale data will be fetched from database,
+     *            ignoring the changes in hibernate layer 2) if you have many write operations
+     *            interleaved with read operations in one block, switching synchronization off
+     *            greatly improves the performance.
+     */
+    public final static void setBatchUpdateMode(Session session, boolean batchMode)
+    {
+        CacheMode cacheMode = (batchMode ? CacheMode.IGNORE : CacheMode.NORMAL);
+        session.setCacheMode(cacheMode);
+
+        FlushMode mode = (batchMode ? FlushMode.COMMIT : FlushMode.AUTO);
+        session.setFlushMode(mode);
+    }
+
 }
-- 
GitLab