01: package org.concern.controller;
02:
03: import org.hibernate.context.*;
04: import org.hibernate.engine.SessionFactoryImplementor;
05: import org.hibernate.classic.Session;
06: import org.hibernate.*;
07: import org.apache.commons.logging.LogFactory;
08:
09: public class ManagableSessionContext implements CurrentSessionContext {
10: private static org.apache.commons.logging.Log LOG = LogFactory
11: .getLog(ManagableSessionContext.class);
12:
13: private static final ThreadLocal context = new ThreadLocal();
14: private final ManagedSessionContext managedSessionContext;
15: private final JTASessionContext jtaSessionContext;
16:
17: public ManagableSessionContext(SessionFactoryImplementor factory) {
18: managedSessionContext = new ManagedSessionContext(factory);
19: jtaSessionContext = new JTASessionContext(factory);
20: }
21:
22: private CurrentSessionContext getSessionContext() {
23: return isManagedMode() ? managedSessionContext
24: : jtaSessionContext;
25: }
26:
27: public Session currentSession() throws HibernateException {
28: return getSessionContext().currentSession();
29: }
30:
31: public static boolean isManagedMode() {
32: return context.get() != null;
33: }
34:
35: public static boolean hasBind(SessionFactory factory) {
36: if (!isManagedMode())
37: LOG.warn("Not in managed mode");
38: return ManagedSessionContext.hasBind(factory);
39: }
40:
41: // TODO: should expect SessionFactory interface or nothing
42: public static Session openSession(SessionFactoryImplementor factory) {
43: context.set(Boolean.TRUE);
44: return ManagedSessionContext.bind(factory.openSession(null,
45: true, false, ConnectionReleaseMode.AFTER_STATEMENT));
46: }
47:
48: public static Session closeSession(SessionFactory factory) {
49: context.remove();
50: Session session = ManagedSessionContext.unbind(factory);
51: if (session != null)
52: session.close();
53: return session;
54: }
55: }
|