changeset 183:222f4db3430a

Importer: Used thread local pattern to make sharing of session easier. flys-backend/trunk@1494 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 17 Mar 2011 10:47:31 +0000
parents 1c0afb01bc93
children 4ab2c3bd474c
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/Importer.java
diffstat 2 files changed, 16 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Mar 17 10:29:18 2011 +0000
+++ b/flys-backend/ChangeLog	Thu Mar 17 10:47:31 2011 +0000
@@ -1,3 +1,8 @@
+2011-03-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/Importer.java:
+	  Used thread local pattern to make sharing of session easier.
+
 2011-03-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/Importer.java:
--- a/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Thu Mar 17 10:29:18 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Thu Mar 17 10:47:31 2011 +0000
@@ -20,10 +20,11 @@
 {
     private static Logger log = Logger.getLogger(Importer.class);
 
+    public static final ThreadLocal<Session> sessionHolder =
+        new ThreadLocal<Session>();
+
     protected List<ImportRiver> rivers;
 
-    protected Session session;
-
     public Importer() {
     }
 
@@ -41,6 +42,8 @@
 
     public void writeRivers() {
         log.debug("write rivers started");
+        Session session = sessionHolder.get();
+
         Query query = session.createQuery("from River where name=:name");
         for (ImportRiver iriver: rivers) {
             log.debug("writing river '" + iriver.getName() + "'");
@@ -58,7 +61,10 @@
         SessionFactory sessionFactory =
             SessionFactoryProvider.createSessionFactory();
 
-        session = sessionFactory.openSession();
+        Session session = sessionFactory.openSession();
+
+        sessionHolder.set(session);
+
         Transaction tx = null;
 
         try {
@@ -75,10 +81,8 @@
             throw re;
         }
         finally {
-            if (session != null) {
-                session.close();
-                session = null;
-            }
+            session.close();
+            sessionHolder.remove();
         }
     }
 

http://dive4elements.wald.intevation.org