changeset 8576:0d711ecbcd49

Renable access to seddb sessions The inheritance / overrides of static methods don't work that way.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 11 Mar 2015 17:44:27 +0100
parents a8048aac8831
children 3fae08a8cb90
files backend/src/main/java/org/dive4elements/river/backend/SedDBSessionHolder.java
diffstat 1 files changed, 29 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/backend/SedDBSessionHolder.java	Tue Mar 10 17:01:42 2015 +0100
+++ b/backend/src/main/java/org/dive4elements/river/backend/SedDBSessionHolder.java	Wed Mar 11 17:44:27 2015 +0100
@@ -13,16 +13,44 @@
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 
-public class SedDBSessionHolder extends SessionHolder
+public class SedDBSessionHolder
 {
     private static Logger log =
         Logger.getLogger(SedDBSessionHolder.class);
 
+    public static final ThreadLocal<Session> HOLDER =
+        new ThreadLocal<Session>() {
+            @Override
+            protected Session initialValue() {
+                Session session = create();
+                log.debug("Initial session value: " + session.hashCode());
+                return session;
+            }
+        };
+
     public synchronized static Session create() {
         log.debug("create");
         SessionFactory sessionFactory =
             SessionFactoryProvider.getSedDBSessionFactory();
         return sessionFactory.openSession();
     }
+
+    public static Session acquire() {
+        Session session = create();
+        log.debug("acquired session: " + session.hashCode());
+        HOLDER.set(session);
+        return session;
+    }
+
+    public static void release() {
+        Session session = HOLDER.get();
+        if (session != null) {
+            log.debug("releasing session: " + session.hashCode());
+            session.close();
+        } else {
+            log.error("release() called on NULL session.");
+        }
+        HOLDER.remove();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org