changeset 466:8bd50b41dea6

Moved the SessionHolder from flys-artifacts to flys-backend. flys-backend/trunk@1700 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 15 Apr 2011 09:49:21 +0000
parents c2c3ad4fda58
children c8c09e31cdb8
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/backend/SessionHolder.java
diffstat 2 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Apr 14 13:43:53 2011 +0000
+++ b/flys-backend/ChangeLog	Fri Apr 15 09:49:21 2011 +0000
@@ -1,3 +1,9 @@
+2011-04-15  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/backend/SessionHolder.java: Moved to
+	  this module from flys-artifacts. This is necessary to get access to the
+	  current session in this module as well.
+
 2011-04-14  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/model/River.java: Added a method that
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionHolder.java	Fri Apr 15 09:49:21 2011 +0000
@@ -0,0 +1,43 @@
+package de.intevation.flys.backend;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+
+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.getSessionFactory();
+        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 :

http://dive4elements.wald.intevation.org