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.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.security.lang.DoPrivilegedUtil;
023 import com.liferay.portal.spring.hibernate.PortletHibernateConfiguration;
024 import com.liferay.portal.util.PropsValues;
025
026 import java.sql.Connection;
027
028 import javax.sql.DataSource;
029
030 import org.hibernate.SessionFactory;
031
032
036 public class PortletSessionFactoryImpl extends SessionFactoryImpl {
037
038 @Override
039 public void closeSession(Session session) throws ORMException {
040 if (session != null) {
041 session.flush();
042
043 if (!PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
044 session.close();
045 }
046 }
047 }
048
049 @Override
050 public Session openSession() throws ORMException {
051 SessionFactory sessionFactory = getSessionFactory();
052
053 org.hibernate.Session session = null;
054
055 if (PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED) {
056 Connection connection = CurrentConnectionUtil.getConnection(
057 getDataSource());
058
059 if (connection == null) {
060 session = sessionFactory.getCurrentSession();
061 }
062 else {
063 session = sessionFactory.openSession(connection);
064 }
065 }
066 else {
067 session = sessionFactory.openSession();
068 }
069
070 if (_log.isDebugEnabled()) {
071 org.hibernate.impl.SessionImpl sessionImpl =
072 (org.hibernate.impl.SessionImpl)session;
073
074 _log.debug(
075 "Session is using connection release mode " +
076 sessionImpl.getConnectionReleaseMode());
077 }
078
079 return wrapSession(session);
080 }
081
082 public void setDataSource(DataSource dataSource) {
083 _dataSource = dataSource;
084 }
085
086 protected SessionFactory createSessionFactory(DataSource dataSource) {
087 PortletHibernateConfiguration portletHibernateConfiguration =
088 new PortletHibernateConfiguration(
089 getSessionFactoryClassLoader(), dataSource);
090
091 portletHibernateConfiguration.setDataSource(dataSource);
092
093 SessionFactory sessionFactory = null;
094
095 try {
096 sessionFactory =
097 portletHibernateConfiguration.buildSessionFactory();
098 }
099 catch (Exception e) {
100 _log.error(e, e);
101
102 return null;
103 }
104
105 return sessionFactory;
106 }
107
108 protected DataSource getDataSource() {
109 return _dataSource;
110 }
111
112 protected SessionFactory getSessionFactory() {
113 return getSessionFactoryImplementor();
114 }
115
116 @Override
117 protected Session wrapSession(org.hibernate.Session session) {
118 return DoPrivilegedUtil.wrapWhenActive(super.wrapSession(session));
119 }
120
121 private static final Log _log = LogFactoryUtil.getLog(
122 PortletSessionFactoryImpl.class);
123
124 private DataSource _dataSource;
125
126 }