Mercurial > dive4elements > river
changeset 307:84f8462790cb
Added an Implementation of CallContext.Listener to open/close Hibernate Sessions for each request.
flys-artifacts/trunk@1687 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 14 Apr 2011 11:18:28 +0000 (2011-04-14) |
parents | 35301cc3b875 |
children | 084995dd1c9c |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java |
diffstat | 2 files changed, 77 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Thu Apr 14 08:32:18 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Apr 14 11:18:28 2011 +0000 @@ -1,3 +1,9 @@ +2011-04-14 Ingo Weinzierl <ingo@intevation.de> + + * src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java: + Implementation of a CallContext.Listener to open/close Hibernate + Sessions for each request. + 2011-04-14 Ingo Weinzierl <ingo@intevation.de> * src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/SessionCallContextListener.java Thu Apr 14 11:18:28 2011 +0000 @@ -0,0 +1,71 @@ +package de.intevation.flys.artifacts.context; + +import org.apache.log4j.Logger; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import de.intevation.flys.backend.SessionFactoryProvider; + +import de.intevation.artifacts.CallContext; +import de.intevation.artifacts.CallContext.Listener; + + +/** + * This CallContextListener is used to initialize a ThreadLocal variable in + * each CallContext (for each request) that holds Sessions. + * + * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> + */ +public class SessionCallContextListener implements Listener { + + public static final String SESSION_HOLDER_KEY = "context.session.holder"; + + /** The logger that is used in this class.*/ + private static Logger logger = + Logger.getLogger(SessionCallContextListener.class); + + + public SessionCallContextListener() { + } + + + /** + * Initializes a ThreadLocal variable that is used to hold sessions. + * + * @param context The CallContext. + */ + public void init(CallContext context) { + logger.debug("SessionCallContextListener.init"); + + ThreadLocal<Session> sessionHolder = new ThreadLocal<Session>(); + + SessionFactory sessionFactory = + SessionFactoryProvider.createSessionFactory(); + + Session session = sessionFactory.openSession(); + + sessionHolder.set(session); + + context.putContextValue(SESSION_HOLDER_KEY, sessionHolder); + } + + + /** + * Closes open sessions of the ThreadLocal variable opened in init(). + * + * @param context The CallContext. + */ + public void close(CallContext context) { + logger.debug("SessionCallContextListener.close"); + + ThreadLocal<Session> sessionHolder = (ThreadLocal<Session>) + context.getContextValue(SESSION_HOLDER_KEY); + + Session session = sessionHolder.get(); + + session.close(); + sessionHolder.remove(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :