001
014
015 package com.liferay.portal.dao.orm.hibernate;
016
017 import com.liferay.portal.kernel.dao.orm.ClassLoaderSession;
018 import com.liferay.portal.kernel.dao.orm.Dialect;
019 import com.liferay.portal.kernel.dao.orm.ORMException;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.SessionFactory;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.util.PropsValues;
025
026 import java.sql.Connection;
027
028 import java.util.List;
029 import java.util.concurrent.CopyOnWriteArrayList;
030
031 import org.hibernate.engine.SessionFactoryImplementor;
032
033
037 public class SessionFactoryImpl implements SessionFactory {
038
039 public static List<PortletSessionFactoryImpl> getPortletSessionFactories() {
040 return portletSessionFactories;
041 }
042
043 public void closeSession(Session session) throws ORMException {
044 if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
045 session.flush();
046 session.close();
047 }
048 }
049
050 public void destroy() {
051 portletSessionFactories.clear();
052 }
053
054 public Session getCurrentSession() throws ORMException {
055 return wrapSession(_sessionFactoryImplementor.getCurrentSession());
056 }
057
058 public Dialect getDialect() throws ORMException {
059 return new DialectImpl(_sessionFactoryImplementor.getDialect());
060 }
061
062 public ClassLoader getSessionFactoryClassLoader() {
063 return _sessionFactoryClassLoader;
064 }
065
066 public SessionFactoryImplementor getSessionFactoryImplementor() {
067 return _sessionFactoryImplementor;
068 }
069
070 public Session openNewSession(Connection connection) throws ORMException {
071 return wrapSession(_sessionFactoryImplementor.openSession(connection));
072 }
073
074 public Session openSession() throws ORMException {
075 org.hibernate.Session session = null;
076
077 if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
078 session = _sessionFactoryImplementor.getCurrentSession();
079 }
080 else {
081 session = _sessionFactoryImplementor.openSession();
082 }
083
084 if (_log.isDebugEnabled()) {
085 org.hibernate.impl.SessionImpl sessionImpl =
086 (org.hibernate.impl.SessionImpl)session;
087
088 _log.debug(
089 "Session is using connection release mode " +
090 sessionImpl.getConnectionReleaseMode());
091 }
092
093 return wrapSession(session);
094 }
095
096 public void setSessionFactoryClassLoader(
097 ClassLoader sessionFactoryClassLoader) {
098
099 _sessionFactoryClassLoader = sessionFactoryClassLoader;
100 }
101
102 public void setSessionFactoryImplementor(
103 SessionFactoryImplementor sessionFactoryImplementor) {
104
105 _sessionFactoryImplementor = sessionFactoryImplementor;
106 }
107
108 protected Session wrapSession(org.hibernate.Session session) {
109 Session liferaySession = new SessionImpl(session);
110
111 if (_sessionFactoryClassLoader != null) {
112
113
114
115 liferaySession = new ClassLoaderSession(
116 liferaySession, _sessionFactoryClassLoader);
117 }
118
119 return liferaySession;
120 }
121
122 private static Log _log = LogFactoryUtil.getLog(SessionFactoryImpl.class);
123
124 protected static final List<PortletSessionFactoryImpl>
125 portletSessionFactories =
126 new CopyOnWriteArrayList<PortletSessionFactoryImpl>();
127
128 private ClassLoader _sessionFactoryClassLoader;
129 private SessionFactoryImplementor _sessionFactoryImplementor;
130
131 }