Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java @ 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 | 6d2c8915e22c |
comparison
equal
deleted
inserted
replaced
307:84f8462790cb | 308:084995dd1c9c |
---|---|
1 package de.intevation.flys.artifacts.context; | 1 package de.intevation.flys.artifacts.context; |
2 | 2 |
3 import org.apache.log4j.Logger; | 3 import org.apache.log4j.Logger; |
4 | 4 |
5 import org.hibernate.Session; | 5 import org.hibernate.Session; |
6 import org.hibernate.SessionFactory; | |
7 | |
8 import de.intevation.flys.backend.SessionFactoryProvider; | |
9 | 6 |
10 import de.intevation.artifacts.CallContext; | 7 import de.intevation.artifacts.CallContext; |
11 import de.intevation.artifacts.CallContext.Listener; | 8 import de.intevation.artifacts.CallContext.Listener; |
9 | |
10 import de.intevation.flys.artifacts.model.SessionHolder; | |
12 | 11 |
13 | 12 |
14 /** | 13 /** |
15 * This CallContextListener is used to initialize a ThreadLocal variable in | 14 * This CallContextListener is used to initialize a ThreadLocal variable in |
16 * each CallContext (for each request) that holds Sessions. | 15 * each CallContext (for each request) that holds Sessions. |
17 * | 16 * |
18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 17 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
19 */ | 18 */ |
20 public class SessionCallContextListener implements Listener { | 19 public class SessionCallContextListener implements Listener { |
21 | 20 |
22 public static final String SESSION_HOLDER_KEY = "context.session.holder"; | 21 public static final String SESSION_KEY = "context.session"; |
23 | 22 |
24 /** The logger that is used in this class.*/ | 23 /** The logger that is used in this class.*/ |
25 private static Logger logger = | 24 private static Logger logger = |
26 Logger.getLogger(SessionCallContextListener.class); | 25 Logger.getLogger(SessionCallContextListener.class); |
27 | 26 |
36 * @param context The CallContext. | 35 * @param context The CallContext. |
37 */ | 36 */ |
38 public void init(CallContext context) { | 37 public void init(CallContext context) { |
39 logger.debug("SessionCallContextListener.init"); | 38 logger.debug("SessionCallContextListener.init"); |
40 | 39 |
41 ThreadLocal<Session> sessionHolder = new ThreadLocal<Session>(); | 40 Session session = SessionHolder.acquire(); |
42 | 41 |
43 SessionFactory sessionFactory = | 42 context.putContextValue(SESSION_KEY, session); |
44 SessionFactoryProvider.createSessionFactory(); | |
45 | |
46 Session session = sessionFactory.openSession(); | |
47 | |
48 sessionHolder.set(session); | |
49 | |
50 context.putContextValue(SESSION_HOLDER_KEY, sessionHolder); | |
51 } | 43 } |
52 | 44 |
53 | 45 |
54 /** | 46 /** |
55 * Closes open sessions of the ThreadLocal variable opened in init(). | 47 * Closes open sessions of the ThreadLocal variable opened in init(). |
57 * @param context The CallContext. | 49 * @param context The CallContext. |
58 */ | 50 */ |
59 public void close(CallContext context) { | 51 public void close(CallContext context) { |
60 logger.debug("SessionCallContextListener.close"); | 52 logger.debug("SessionCallContextListener.close"); |
61 | 53 |
62 ThreadLocal<Session> sessionHolder = (ThreadLocal<Session>) | 54 Session session = (Session)context.getContextValue(SESSION_KEY); |
63 context.getContextValue(SESSION_HOLDER_KEY); | 55 session.close(); |
64 | 56 |
65 Session session = sessionHolder.get(); | 57 SessionHolder.release(); |
66 | |
67 session.close(); | |
68 sessionHolder.remove(); | |
69 } | 58 } |
70 } | 59 } |
71 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 60 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |