001
014
015 package com.liferay.portal.dao.orm.hibernate;
016
017 import com.liferay.portal.kernel.dao.jdbc.CurrentConnectionUtil;
018 import com.liferay.portal.kernel.dao.orm.ORMException;
019 import com.liferay.portal.kernel.dao.orm.Session;
020 import com.liferay.portal.kernel.dao.shard.ShardDataSourceTargetSource;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.InfrastructureUtil;
024 import com.liferay.portal.security.lang.DoPrivilegedUtil;
025 import com.liferay.portal.spring.hibernate.PortletHibernateConfiguration;
026 import com.liferay.portal.util.PropsValues;
027
028 import java.sql.Connection;
029
030 import java.util.HashMap;
031 import java.util.Map;
032
033 import javax.sql.DataSource;
034
035 import org.hibernate.SessionFactory;
036
037
041 public class PortletSessionFactoryImpl extends SessionFactoryImpl {
042
043 @Override
044 public void closeSession(Session session) throws ORMException {
045 if (session != null) {
046 session.flush();
047
048 if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
049 session.close();
050 }
051 }
052 }
053
054 @Override
055 public Session openSession() throws ORMException {
056 SessionFactory sessionFactory = getSessionFactory();
057
058 org.hibernate.Session session = null;
059
060 if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
061 Connection connection = CurrentConnectionUtil.getConnection(
062 getDataSource());
063
064 if (connection == null) {
065 session = sessionFactory.getCurrentSession();
066 }
067 else {
068 session = sessionFactory.openSession(connection);
069 }
070 }
071 else {
072 session = sessionFactory.openSession();
073 }
074
075 if (_log.isDebugEnabled()) {
076 org.hibernate.impl.SessionImpl sessionImpl =
077 (org.hibernate.impl.SessionImpl)session;
078
079 _log.debug(
080 "Session is using connection release mode " +
081 sessionImpl.getConnectionReleaseMode());
082 }
083
084 return wrapSession(session);
085 }
086
087 public void setDataSource(DataSource dataSource) {
088 _dataSource = dataSource;
089 }
090
091 protected SessionFactory createSessionFactory(DataSource dataSource) {
092 PortletHibernateConfiguration portletHibernateConfiguration =
093 new PortletHibernateConfiguration(
094 getSessionFactoryClassLoader(), dataSource);
095
096 portletHibernateConfiguration.setDataSource(dataSource);
097
098 SessionFactory sessionFactory = null;
099
100 try {
101 sessionFactory =
102 portletHibernateConfiguration.buildSessionFactory();
103 }
104 catch (Exception e) {
105 _log.error(e, e);
106
107 return null;
108 }
109
110 return sessionFactory;
111 }
112
113 protected DataSource getDataSource() {
114 ShardDataSourceTargetSource shardDataSourceTargetSource =
115 InfrastructureUtil.getShardDataSourceTargetSource();
116
117 if (shardDataSourceTargetSource != null) {
118 return shardDataSourceTargetSource.getDataSource();
119 }
120
121 return _dataSource;
122 }
123
124 protected SessionFactory getSessionFactory() {
125 ShardDataSourceTargetSource shardDataSourceTargetSource =
126 InfrastructureUtil.getShardDataSourceTargetSource();
127
128 if (shardDataSourceTargetSource == null) {
129 return getSessionFactoryImplementor();
130 }
131
132 DataSource dataSource = shardDataSourceTargetSource.getDataSource();
133
134 SessionFactory sessionFactory = getSessionFactory(dataSource);
135
136 if (sessionFactory != null) {
137 return sessionFactory;
138 }
139
140 sessionFactory = createSessionFactory(dataSource);
141
142 if (sessionFactory != null) {
143 putSessionFactory(dataSource, sessionFactory);
144 }
145
146 return sessionFactory;
147 }
148
149 protected SessionFactory getSessionFactory(DataSource dataSource) {
150 return _sessionFactories.get(dataSource);
151 }
152
153 protected void putSessionFactory(
154 DataSource dataSource, SessionFactory sessionFactory) {
155
156 _sessionFactories.put(dataSource, sessionFactory);
157 }
158
159 @Override
160 protected Session wrapSession(org.hibernate.Session session) {
161 return DoPrivilegedUtil.wrapWhenActive(super.wrapSession(session));
162 }
163
164 private static final Log _log = LogFactoryUtil.getLog(
165 PortletSessionFactoryImpl.class);
166
167 private DataSource _dataSource;
168 private final Map<DataSource, SessionFactory> _sessionFactories =
169 new HashMap<>();
170
171 }