2014年11月26日 星期三

Liferay 同時連接不同資料庫

更改額外DB
1.建立portal-ext.properties (\tomcat-7.0.27\webapps\ROOT\WEB-INF\classes)
2.建立service.xml and bulid
3.建立ext-spring.xml (文件路徑/ WEB-INF/src/META-INF/ext-spring.xml) and bulid



portal-ext.properties 如下

jdbc.default.url=jdbc\:mysql\://localhost/liferay612?useUnicode\=true&characterEncoding\=UTF-8&useFastDateParsing\=false
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.username=root
jdbc.default.password=root
jdbc.test .driverClassName=com.mysql.jdbc.Driver
jdbc.test .url=jdbc:mysql://localhost/testE?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.test .username=root
jdbc.test .password=root


service.xml 如下
<entity name="TableFromAnotherDataSource" table="TableFromAnotherDataSource" local-service="true" remote-service="true" data-source="anotherDataSource" session-factory="anotherSessionFactory">
<column name="testId" type="long" primary="true" />
<column name="description" type="String" />
/entity>
資料庫的資料表要自己建,額外DB不會自動建。



ext-spring.xml file 如下






<?xml version="1.0"?>
<beans default-destroy-method="destroy" default-init-method="afterPropertiesSet"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="anotherDataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="anotherDataSourceWrapper" />
</bean>
<bean id="anotherDataSourceImpl"
class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
<property name="propertyPrefix" value="jdbc.test." />
</bean>
<bean id="anotherDataSourceWrapper" class="com.liferay.portal.dao.jdbc.util.DataSourceWrapper">
<constructor-arg ref="anotherDataSourceImpl" />
</bean>
<bean class="com.liferay.portal.dao.jdbc.util.DataSourceSwapper">
<property name="liferayDataSourceWrapper" ref="anotherDataSourceWrapper" />
</bean>
<bean id="anotherHibernateSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
factory-method="newBean">
<constructor-arg
value="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" />
<constructor-arg>
<map>
<entry key="dataSource" value-ref="anotherDataSource" />
</map>
</constructor-arg>
</bean>
<bean id="anotherSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
factory-method="newBean">
<constructor-arg
value="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl" />
<constructor-arg>
<map>
<entry key="dataSource" value-ref="anotherDataSource" />
<entry key="sessionFactoryClassLoader" value-ref="portletClassLoader" />
<entry key="sessionFactoryImplementor" value-ref="anotherHibernateSessionFactory" />
</map>
</constructor-arg>
</bean>
</beans>

JAVA

TableFromAnotherDataSourceImpl tableFromDefaultDataSource = new TableFromAnotherDataSourceImpl();
long testId = CounterLocalServiceUtil.increment(TableFromAnotherDataSourceImpl.class.getName());
tableFromDefaultDataSource.setTestId(testId);
tableFromDefaultDataSource.setDescription("hi");
TableFromAnotherDataSourceLocalServiceUtil.addTableFromAnotherDataSource(tableFromDefaultDataSource);

沒有留言:

張貼留言