Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java @ 1072:80aecb01d79a
Let MainValuesArtifact have a state, outputmode and facet.
flys-artifacts/trunk@2556 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 24 Aug 2011 12:07:33 +0000 |
parents | 94c176fdcb89 |
children |
rev | line source |
---|---|
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.context; |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
2 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
3 import org.apache.log4j.Logger; |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
4 |
310
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
5 import org.w3c.dom.Document; |
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
6 import org.w3c.dom.Node; |
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
7 |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
8 import org.hibernate.Session; |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
9 |
315
94c176fdcb89
Removed the SessionHolder and adapted imports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
310
diff
changeset
|
10 import de.intevation.flys.backend.SessionHolder; |
94c176fdcb89
Removed the SessionHolder and adapted imports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
310
diff
changeset
|
11 |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
12 import de.intevation.artifacts.CallContext; |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
13 import de.intevation.artifacts.CallContext.Listener; |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
14 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
15 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
16 /** |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
17 * This CallContextListener is used to initialize a ThreadLocal variable in |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
18 * each CallContext (for each request) that holds Sessions. |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 * |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
20 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
21 */ |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
22 public class SessionCallContextListener implements Listener { |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
23 |
308
084995dd1c9c
Added a session holder to hold hibernate sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
24 public static final String SESSION_KEY = "context.session"; |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
25 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
26 /** The logger that is used in this class.*/ |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
27 private static Logger logger = |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
28 Logger.getLogger(SessionCallContextListener.class); |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
29 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
30 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
31 public SessionCallContextListener() { |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
32 } |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
33 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
34 |
310
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
35 public void setup(Document config, Node listenerConfig) { |
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
36 // nothing to do here |
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
37 } |
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
38 |
6d2c8915e22c
Finalized the SessionCallContextListener and registered it as CallContext.Listener in the configuration.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
308
diff
changeset
|
39 |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
40 /** |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
41 * Initializes a ThreadLocal variable that is used to hold sessions. |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
42 * |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
43 * @param context The CallContext. |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
44 */ |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
45 public void init(CallContext context) { |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
46 logger.debug("SessionCallContextListener.init"); |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
47 |
308
084995dd1c9c
Added a session holder to hold hibernate sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
48 Session session = SessionHolder.acquire(); |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
49 |
308
084995dd1c9c
Added a session holder to hold hibernate sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
50 context.putContextValue(SESSION_KEY, session); |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
51 } |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
52 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
53 |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
54 /** |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
55 * Closes open sessions of the ThreadLocal variable opened in init(). |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
56 * |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
57 * @param context The CallContext. |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
58 */ |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
59 public void close(CallContext context) { |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
60 logger.debug("SessionCallContextListener.close"); |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
61 |
308
084995dd1c9c
Added a session holder to hold hibernate sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
62 Session session = (Session)context.getContextValue(SESSION_KEY); |
084995dd1c9c
Added a session holder to hold hibernate sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
63 session.close(); |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
64 |
308
084995dd1c9c
Added a session holder to hold hibernate sessions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
65 SessionHolder.release(); |
307
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
66 } |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
67 } |
84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
68 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |