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.util.PropsValues;
33
34 import java.lang.reflect.Proxy;
35
36 import java.sql.Connection;
37
38 import org.hibernate.engine.SessionFactoryImplementor;
39
40
45 public class SessionFactoryImpl implements SessionFactory {
46
47 public void closeSession(Session session) throws ORMException {
48 if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
49 session.close();
50 }
51 }
52
53 public Dialect getDialect() throws ORMException {
54 return new DialectImpl(_sessionFactoryImplementor.getDialect());
55 }
56
57 public SessionFactoryImplementor getSessionFactoryImplementor() {
58 return _sessionFactoryImplementor;
59 }
60
61 public Session openNewSession(Connection connection) throws ORMException {
62 return transformSession(
63 _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 LiferayClassicSession classicSession =
78 (LiferayClassicSession)session;
79
80 org.hibernate.impl.SessionImpl sessionImpl =
81 (org.hibernate.impl.SessionImpl)
82 classicSession.getHibernateClassicSession();
83
84 _log.debug(
85 "Session is using connection release mode " +
86 sessionImpl.getConnectionReleaseMode());
87 }
88
89 return transformSession(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 transformSession(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 }