1
14
15 package com.liferay.portal.dao.orm.hibernate;
16
17 import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
18 import com.liferay.portal.kernel.dao.orm.Dialect;
19 import com.liferay.portal.kernel.dao.orm.ORMException;
20 import com.liferay.portal.kernel.dao.orm.Session;
21 import com.liferay.portal.kernel.dao.orm.SessionFactory;
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.util.PropsValues;
25
26 import java.lang.reflect.Proxy;
27
28 import java.sql.Connection;
29
30 import org.hibernate.engine.SessionFactoryImplementor;
31
32
37 public class SessionFactoryImpl implements SessionFactory {
38
39 public void closeSession(Session session) throws ORMException {
40 if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
41 session.close();
42 }
43 }
44
45 public Dialect getDialect() throws ORMException {
46 return new DialectImpl(_sessionFactoryImplementor.getDialect());
47 }
48
49 public SessionFactoryImplementor getSessionFactoryImplementor() {
50 return _sessionFactoryImplementor;
51 }
52
53 public Session openNewSession(Connection connection) throws ORMException {
54 return wrapSession(_sessionFactoryImplementor.openSession(connection));
55 }
56
57 public Session openSession() throws ORMException {
58 org.hibernate.Session session = null;
59
60 if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
61 session = _sessionFactoryImplementor.getCurrentSession();
62 }
63 else {
64 session = _sessionFactoryImplementor.openSession();
65 }
66
67 if (_log.isDebugEnabled()) {
68 org.hibernate.impl.SessionImpl sessionImpl =
69 (org.hibernate.impl.SessionImpl)session;
70
71 _log.debug(
72 "Session is using connection release mode " +
73 sessionImpl.getConnectionReleaseMode());
74 }
75
76 return wrapSession(session);
77 }
78
79 public void setSessionFactoryClassLoader(
80 ClassLoader sessionFactoryClassLoader) {
81
82 _sessionFactoryClassLoader = sessionFactoryClassLoader;
83 }
84
85 public void setSessionFactoryImplementor(
86 SessionFactoryImplementor sessionFactoryImplementor) {
87
88 _sessionFactoryImplementor = sessionFactoryImplementor;
89 }
90
91 protected Session wrapSession(org.hibernate.Session session) {
92 Session liferaySession = new SessionImpl(session);
93
94 if (_sessionFactoryClassLoader != null) {
95
96
98 liferaySession = (Session)Proxy.newProxyInstance(
99 _sessionFactoryClassLoader,
100 new Class[] {Session.class},
101 new ClassLoaderBeanHandler(
102 liferaySession, _sessionFactoryClassLoader));
103 }
104
105 return liferaySession;
106 }
107
108 private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
109
110 private ClassLoader _sessionFactoryClassLoader;
111 private SessionFactoryImplementor _sessionFactoryImplementor;
112
113 }