changeset 308:084995dd1c9c

Added a session holder to hold hibernate sessions. flys-artifacts/trunk@1689 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 14 Apr 2011 12:08:47 +0000
parents 84f8462790cb
children e91873b8b937
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SessionHolder.java flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java
diffstat 5 files changed, 68 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Apr 14 11:18:28 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Apr 14 12:08:47 2011 +0000
@@ -1,3 +1,16 @@
+2011-04-14	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/SessionHolder.java:
+	  New. Contains thread local session holder for hibernate sessions.
+	* src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java:
+	  Uses session from SessionHolder.
+
+	* src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java:
+	  Interacts with SessionHolder now.
+	
+	* src/main/java/de/intevation/flys/exports/ChartExportHelper.java:
+	  Removed superfluous import.
+	
 2011-04-14  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java	Thu Apr 14 11:18:28 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java	Thu Apr 14 12:08:47 2011 +0000
@@ -3,13 +3,12 @@
 import org.apache.log4j.Logger;
 
 import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-
-import de.intevation.flys.backend.SessionFactoryProvider;
 
 import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallContext.Listener;
 
+import de.intevation.flys.artifacts.model.SessionHolder;
+
 
 /**
  * This CallContextListener is used to initialize a ThreadLocal variable in
@@ -19,7 +18,7 @@
  */
 public class SessionCallContextListener implements Listener {
 
-    public static final String SESSION_HOLDER_KEY = "context.session.holder";
+    public static final String SESSION_KEY = "context.session";
 
     /** The logger that is used in this class.*/
     private static Logger logger =
@@ -38,16 +37,9 @@
     public void init(CallContext context) {
         logger.debug("SessionCallContextListener.init");
 
-        ThreadLocal<Session> sessionHolder = new ThreadLocal<Session>();
-
-        SessionFactory sessionFactory =
-            SessionFactoryProvider.createSessionFactory();
+        Session session = SessionHolder.acquire();
 
-        Session session = sessionFactory.openSession();
-
-        sessionHolder.set(session);
-
-        context.putContextValue(SESSION_HOLDER_KEY, sessionHolder);
+        context.putContextValue(SESSION_KEY, session);
     }
 
 
@@ -59,13 +51,10 @@
     public void close(CallContext context) {
         logger.debug("SessionCallContextListener.close");
 
-        ThreadLocal<Session> sessionHolder = (ThreadLocal<Session>)
-            context.getContextValue(SESSION_HOLDER_KEY);
+        Session session = (Session)context.getContextValue(SESSION_KEY);
+        session.close();
 
-        Session session = sessionHolder.get();
-
-        session.close();
-        sessionHolder.remove();
+        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/model/RiverFactory.java	Thu Apr 14 11:18:28 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Apr 14 12:08:47 2011 +0000
@@ -26,15 +26,9 @@
      * @return all rivers.
      */
     public static List<River> getRivers() {
-        SessionFactory sf = SessionFactoryProvider.getSessionFactory();
-        Session session = sf.openSession();
-        try {
-            return (List<River>)session
-                .createQuery("from River order by name").list();
-        }
-        finally {
-            session.close();
-        }
+        Session session = SessionHolder.HOLDER.get();
+        return (List<River>)session
+            .createQuery("from River order by name").list();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SessionHolder.java	Thu Apr 14 12:08:47 2011 +0000
@@ -0,0 +1,44 @@
+package de.intevation.flys.artifacts.model;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+import de.intevation.flys.backend.SessionFactoryProvider;
+
+public class SessionHolder
+{
+    private static Logger logger =
+        Logger.getLogger(SessionHolder.class);
+    
+    public static final ThreadLocal<Session> HOLDER =
+        new ThreadLocal<Session>() {
+            protected Session initialValue() {
+                return create();
+            }
+        };
+
+    private SessionHolder() {
+    }
+
+    public synchronized static Session create() {
+        logger.debug("create");
+        SessionFactory sessionFactory =
+            SessionFactoryProvider.createSessionFactory();
+        return sessionFactory.openSession();
+    }
+
+    public static Session acquire() {
+        logger.debug("acquire");
+        Session session = create();
+        HOLDER.set(session);
+        return session;
+    }
+
+    public static void release() {
+        logger.debug("release");
+        HOLDER.remove();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java	Thu Apr 14 11:18:28 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java	Thu Apr 14 12:08:47 2011 +0000
@@ -23,8 +23,6 @@
 
 import java.awt.geom.Rectangle2D;
 
-import java.awt.image.BufferedImage;
-
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;

http://dive4elements.wald.intevation.org