changeset 1631:26e19cdaed5e

Improved database session handling in services and artifacts. flys-artifacts/trunk@2807 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 22 Sep 2011 10:07:22 +0000
parents cafd8af6734a
children 2aa727cefb0c
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FLYSService.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/RiverService.java
diffstat 7 files changed, 116 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Sep 22 07:58:24 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Sep 22 10:07:22 2011 +0000
@@ -1,3 +1,23 @@
+2011-09-22  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/FLYSService.java:
+	  New. A subclass of DefaultService which is used in FLYS to init and
+	  shutdown database connections.
+
+	* src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java,
+	  src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java,
+	  src/main/java/de/intevation/flys/artifacts/services/RiverService.java:
+	  Centralized the initialization and shutdown of database connections.
+	  These services now subclass FLYSService which handles the database stuff.
+
+	* src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java: The
+	  current database connection is not closed here - this is done in a
+	  Service or in the CallContext.
+
+	* src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java: It's
+	  not necessary to create new database connections here. We already have
+	  an existing connection which is initialized by CallContext.
+
 2011-09-22  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/DistanceOnlySelect.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Thu Sep 22 07:58:24 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Thu Sep 22 10:07:22 2011 +0000
@@ -8,8 +8,6 @@
 
 import org.apache.log4j.Logger;
 
-import org.hibernate.Session;
-
 //import net.sf.ehcache.Cache;
 
 import de.intevation.artifacts.Artifact;
@@ -30,8 +28,6 @@
 import de.intevation.flys.artifacts.context.FLYSContext;
 import de.intevation.flys.artifacts.model.AnnotationsFactory;
 
-import de.intevation.flys.backend.SessionHolder;
-
 import de.intevation.flys.model.Annotation;
 
 import de.intevation.flys.utils.FLYSUtils;
@@ -246,10 +242,8 @@
      */
     protected List<Annotation> getAnnotationsUncached(String river) {
         List<Annotation> annotations = new ArrayList<Annotation>();
-        Session session = SessionHolder.acquire();
-        try {
-            annotations = AnnotationsFactory.getPointAnnotations(river);
-        } finally {session.close(); SessionHolder.release();}
+        annotations = AnnotationsFactory.getPointAnnotations(river);
+
         return annotations;
     }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Sep 22 07:58:24 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Sep 22 10:07:22 2011 +0000
@@ -25,8 +25,8 @@
      */
     public static List<River> getRivers() {
         Session session = SessionHolder.HOLDER.get();
-        return (List<River>)session
-            .createQuery("from River order by name").list();
+
+        return session.createQuery("from River order by name").list();
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java	Thu Sep 22 07:58:24 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java	Thu Sep 22 10:07:22 2011 +0000
@@ -14,9 +14,6 @@
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 import de.intevation.artifacts.common.utils.XMLUtils;
 
-import de.intevation.artifactdatabase.DefaultService;
-
-import de.intevation.flys.backend.SessionHolder;
 import de.intevation.flys.model.Annotation;
 import de.intevation.flys.model.Attribute;
 import de.intevation.flys.model.Position;
@@ -25,8 +22,6 @@
 
 import de.intevation.flys.artifacts.model.AnnotationsFactory;
 
-import org.hibernate.Session;
-
 import de.intevation.flys.artifacts.cache.CacheFactory;
 
 import net.sf.ehcache.Cache;
@@ -36,7 +31,7 @@
  *
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class DistanceInfoService extends DefaultService {
+public class DistanceInfoService extends FLYSService {
 
     private static enum DistanceFilter {
         NONE, LOCATIONS, DISTANCES
@@ -60,7 +55,7 @@
 
 
     @Override
-    public Document process(
+    public Document doProcess(
         Document      data,
         GlobalContext globalContext,
         CallMeta      callMeta
@@ -120,31 +115,23 @@
 
         Document result = XMLUtils.newDocument();
 
-        Session session = SessionHolder.acquire();
-
-        try {
-            Iterator<Annotation> iter =
-                AnnotationsFactory.getAnnotationsIterator(river);
-
-            Element all = result.createElement("distances");
-
-            DistanceFilter filter = getDistanceFilter(filtertype);
+        Iterator<Annotation> iter =
+            AnnotationsFactory.getAnnotationsIterator(river);
 
-            while (iter.hasNext()) {
-                Annotation a = iter.next();
-                Element distance = buildDistanceNode(result, a, filter);
+        Element all = result.createElement("distances");
 
-                if (distance != null) {
-                    all.appendChild(distance);
-                }
-            }
+        DistanceFilter filter = getDistanceFilter(filtertype);
 
-            result.appendChild(all);
+        while (iter.hasNext()) {
+            Annotation a = iter.next();
+            Element distance = buildDistanceNode(result, a, filter);
+
+            if (distance != null) {
+                all.appendChild(distance);
+            }
         }
-        finally {
-            session.close();
-            SessionHolder.release();
-        }
+
+        result.appendChild(all);
 
         return result;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FLYSService.java	Thu Sep 22 10:07:22 2011 +0000
@@ -0,0 +1,58 @@
+package de.intevation.flys.artifacts.services;
+
+import org.w3c.dom.Document;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.GlobalContext;
+
+import de.intevation.artifactdatabase.DefaultService;
+
+import de.intevation.flys.backend.SessionHolder;
+
+
+public abstract class FLYSService extends DefaultService {
+
+    private static final Logger logger = Logger.getLogger(FLYSService.class);
+
+
+    public Document process(
+        Document      data,
+        GlobalContext globalContext,
+        CallMeta      callMeta
+    ) {
+        init();
+
+        try {
+            return doProcess(data, globalContext, callMeta);
+        }
+        finally {
+            shutdown();
+        }
+    }
+
+
+    protected abstract Document doProcess(
+        Document      data,
+        GlobalContext globalContext,
+        CallMeta      callMeta);
+
+
+    protected void init() {
+        logger.debug("init");
+        SessionHolder.acquire();
+    }
+
+
+    protected void shutdown() {
+        logger.debug("shutdown");
+        Session session = SessionHolder.HOLDER.get();
+        session.close();
+
+        SessionHolder.release();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java	Thu Sep 22 07:58:24 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java	Thu Sep 22 10:07:22 2011 +0000
@@ -7,18 +7,13 @@
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-import org.hibernate.Session;
-
 import de.intevation.artifacts.CallMeta;
 import de.intevation.artifacts.GlobalContext;
 
-import de.intevation.artifactdatabase.DefaultService;
-
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 import de.intevation.artifacts.common.utils.XMLUtils;
 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
 
-import de.intevation.flys.backend.SessionHolder;
 import de.intevation.flys.model.Gauge;
 import de.intevation.flys.model.MainValue;
 import de.intevation.flys.model.MainValueType;
@@ -36,7 +31,7 @@
  *
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class MainValuesService extends DefaultService {
+public class MainValuesService extends FLYSService {
 
     /** The logger that is used by this service.*/
     private static Logger logger = Logger.getLogger(MainValuesService.class);
@@ -59,7 +54,7 @@
 
 
     @Override
-    public Document process(
+    public Document doProcess(
         Document      data,
         GlobalContext context,
         CallMeta      callMeta
@@ -171,22 +166,15 @@
             logger.debug("Gauge: " + gauge.getName());
         }
 
-        Session session = SessionHolder.acquire();
-        try {
-            List<MainValue> mainValues = MainValuesFactory.getMainValues(gauge);
-
-            if (mainValues == null || mainValues.isEmpty()) {
-                throw new NullPointerException("No main values found.");
-            }
+        List<MainValue> mainValues = MainValuesFactory.getMainValues(gauge);
 
-            logger.debug(mainValues.size() + " main values found.");
-
-            return mainValues;
+        if (mainValues == null || mainValues.isEmpty()) {
+            throw new NullPointerException("No main values found.");
         }
-        finally {
-            session.close();
-            SessionHolder.release();
-        }
+
+        logger.debug(mainValues.size() + " main values found.");
+
+        return mainValues;
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/RiverService.java	Thu Sep 22 07:58:24 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/RiverService.java	Thu Sep 22 10:07:22 2011 +0000
@@ -15,12 +15,10 @@
 
 import de.intevation.artifactdatabase.DefaultService;
 
-import de.intevation.flys.backend.SessionHolder;
 import de.intevation.flys.model.River;
 
 import de.intevation.flys.artifacts.model.RiverFactory;
 
-import org.hibernate.Session;
 
 /**
  * This service provides information about the supported rivers by this
@@ -28,20 +26,13 @@
  *
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
-public class RiverService extends DefaultService {
+public class RiverService extends FLYSService {
 
     /** The logger used in this service.*/
     private static Logger logger = Logger.getLogger(RiverService.class);
 
 
-    /**
-     * The default constructor.
-     */
-    public RiverService() {
-    }
-
-
-    public Document process(
+    protected Document doProcess(
         Document      data,
         GlobalContext globalContext,
         CallMeta      callMeta
@@ -55,25 +46,18 @@
             ArtifactNamespaceContext.NAMESPACE_URI,
             ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
-        Session session = SessionHolder.acquire();
-        try {
-            List<River> allRivers = RiverFactory.getRivers();
-
-            Element rivers = ec.create("rivers");
+        List<River> allRivers = RiverFactory.getRivers();
 
-            for (River river: allRivers) {
-                Element r = ec.create("river");
-                ec.addAttr(r, "name", river.getName(), true);
+        Element rivers = ec.create("rivers");
 
-                rivers.appendChild(r);
-            }
+        for (River river: allRivers) {
+            Element r = ec.create("river");
+            ec.addAttr(r, "name", river.getName(), true);
 
-            result.appendChild(rivers);
+            rivers.appendChild(r);
         }
-        finally {
-            session.close();
-            SessionHolder.release();
-        }
+
+        result.appendChild(rivers);
 
         return result;
     }

http://dive4elements.wald.intevation.org