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