001
014
015 package com.liferay.portal.dao.jdbc.util;
016
017 import com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl;
018 import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019 import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.spring.hibernate.PortalHibernateConfiguration;
023
024 import java.util.Properties;
025
026 import javax.sql.DataSource;
027
028 import org.hibernate.engine.SessionFactoryImplementor;
029
030 import org.springframework.orm.hibernate3.HibernateTransactionManager;
031 import org.springframework.transaction.support.AbstractPlatformTransactionManager;
032
033
036 public class DataSourceSwapper {
037
038 public static void swapCounterDataSource(Properties properties)
039 throws Exception {
040
041 if (_log.isInfoEnabled()) {
042 _log.info("Create new counter data source");
043 }
044
045 DataSource newDataSource = DataSourceFactoryUtil.initDataSource(
046 properties);
047
048 DataSource oldDataSource =
049 _counterDataSourceWrapper.getWrappedDataSource();
050
051 if (_log.isInfoEnabled()) {
052 _log.info("Set new counter data source");
053 }
054
055 _counterDataSourceWrapper.setWrappedDataSource(newDataSource);
056
057 if (_log.isInfoEnabled()) {
058 _log.info("Destroy old counter data source");
059 }
060
061 DataSourceFactoryUtil.destroyDataSource(oldDataSource);
062
063 if (_log.isInfoEnabled()) {
064 _log.info("Reinitialize Hibernate for new counter data source");
065 }
066
067 _reinitializeHibernate("counterSessionFactory", newDataSource);
068 }
069
070 public static void swapLiferayDataSource(Properties properties)
071 throws Exception {
072
073 if (_log.isInfoEnabled()) {
074 _log.info("Create new liferay data source");
075 }
076
077 DataSource newDataSource = DataSourceFactoryUtil.initDataSource(
078 properties);
079
080 DataSource oldDataSource =
081 _liferayDataSourceWrapper.getWrappedDataSource();
082
083 if (_log.isInfoEnabled()) {
084 _log.info("Set new liferay data source");
085 }
086
087 _liferayDataSourceWrapper.setWrappedDataSource(newDataSource);
088
089 if (_log.isInfoEnabled()) {
090 _log.info("Destroy old liferay data source");
091 }
092
093 DataSourceFactoryUtil.destroyDataSource(oldDataSource);
094
095 if (_log.isInfoEnabled()) {
096 _log.info("Reinitialize Hibernate for new liferay data source");
097 }
098
099 _reinitializeHibernate("liferaySessionFactory", newDataSource);
100 }
101
102 public void setCounterDataSourceWrapper(
103 DataSourceWrapper counterDataSourceWrapper) {
104
105 _counterDataSourceWrapper = counterDataSourceWrapper;
106 }
107
108 public void setLiferayDataSourceWrapper(
109 DataSourceWrapper liferayDataSourceWrapper) {
110
111 _liferayDataSourceWrapper = liferayDataSourceWrapper;
112 }
113
114 private static void _reinitializeHibernate(
115 String name, DataSource dataSource)
116 throws Exception {
117
118 PortalHibernateConfiguration portalHibernateConfiguration =
119 new PortalHibernateConfiguration();
120
121 portalHibernateConfiguration.setDataSource(dataSource);
122
123 portalHibernateConfiguration.afterPropertiesSet();
124
125 SessionFactoryImplementor sessionFactoryImplementor =
126 (SessionFactoryImplementor)portalHibernateConfiguration.getObject();
127
128 SessionFactoryImpl sessionFactoryImpl =
129 (SessionFactoryImpl)PortalBeanLocatorUtil.locate(name);
130
131 sessionFactoryImpl.setSessionFactoryImplementor(
132 sessionFactoryImplementor);
133
134 AbstractPlatformTransactionManager abstractPlatformTransactionManager =
135 (AbstractPlatformTransactionManager)PortalBeanLocatorUtil.locate(
136 "liferayTransactionManager");
137
138 if (abstractPlatformTransactionManager instanceof
139 HibernateTransactionManager) {
140
141 HibernateTransactionManager hibernateTransactionManager =
142 (HibernateTransactionManager)abstractPlatformTransactionManager;
143
144 hibernateTransactionManager.setSessionFactory(
145 sessionFactoryImplementor);
146 }
147 else if (_log.isWarnEnabled()) {
148 _log.warn(
149 "Unable to swap to session factory for " +
150 abstractPlatformTransactionManager.getClass() +
151 " which may cause subsequent transaction failures");
152 }
153 }
154
155 private static final Log _log = LogFactoryUtil.getLog(
156 DataSourceSwapper.class);
157
158 private static DataSourceWrapper _counterDataSourceWrapper;
159 private static DataSourceWrapper _liferayDataSourceWrapper;
160
161 }