[PATCH] Renable access to seddb sessions

Wald Commits scm-commit at wald.intevation.org
Wed Mar 11 17:44:31 CET 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1426092267 -3600
# Node ID 0d711ecbcd49a23d41b3e64e5f96bb731951359c
# Parent  a8048aac8831d92b37ba3ab39a7738cde6ca0f18
Renable access to seddb sessions

    The inheritance / overrides of static methods don't work
    that way.

diff -r a8048aac8831 -r 0d711ecbcd49 backend/src/main/java/org/dive4elements/river/backend/SedDBSessionHolder.java
--- 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 :


More information about the Dive4Elements-commits mailing list