Sunday, 13 September 2015

Multiple data source at a time in Liferay


We want to access data from liferay and another database using liferay.

we can achieve multiple database connectivity by the following steps.

Create ext-spring.xml file path /WEB-INF/src/META-INF/ext-spring.xml
Create Service.xml

Modifying the ext-spring.xml
---------------------------------------
Define the userdefined datasource “myDatasource” instead of pointing it into liferay defaultdatasource.
this entry has to be created in the ext-spring.xml shown bellow

<?xml version=”1.0″?>

<beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” default-destroy-method=”destroy” default-init-method=”afterPropertiesSet” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
<bean id=”myDatasource” lazy-init=”true”  class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=”driverClassName” value=”com.mysql.jdbc.Driver” />
<property name=”url” value=”jdbc:mysql://localhost/training?useUnicode=true”/>
<property name=”username” value=”root” />
<property name=”password” value=”root” />
</bean>
<bean id=”liferayHibernateSessionFactory”   class=”com.liferay.portal.spring.hibernate.PortletHibernateConfiguration”>
<property name=”dataSource” ref=”myDatasource” />
</bean>
<bean id="testHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" lazy-init="true">
        <property name="dataSource" ref="testDataSource" />      
    </bean>
  

    <bean id="testSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">      
        <property name="sessionFactoryImplementor" ref="testHibernateSessionFactory" />
    </bean>
  
    <bean id="testTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
        <property name="dataSource" ref="testDataSource" />
        <property name="globalRollbackOnParticipationFailure" value="false"/>
        <property name="sessionFactory" ref="testHibernateSessionFactory" />
    </bean>
</beans>

Adding the Data source in Service.xml
-------------------------------------

We need to tell the service.xml that this entity will use the user defined data source “myDatasource” instead of pointing it into liferay default data source.

Service.xml entry looks like this

<service-builder package-path=”com.liferaytips”>
<namespace>test</namespace>
<entity name=”Training” local-service=”true” table=”training” remote-service=”true” data-source=”myDatasource” session-factory="testSessionFactory" tx-manager="testTransactionManager">
<column name=”userId” type=”long” primary=”true”></column>
........................................
...................................
</entity></service-builder>
Run Build-service
Deploy the portlet
Now your portlet will fetch data from different database table through the liferay service API.

No comments:

Post a Comment