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