Tuesday 11 December 2012

Use other database (beside liferay default) for portlet

Introduction: It is a common requirement, that sometimes we are asked to use entirely a different database for all our backend process rather than the one we have for liferay(the one we generally specify in portal-ext.properties).

Ingredients: prior knowledge of service builder, database with name "myDB" username-"root" password-"root", table "myUserTable"with columns "user_id" and "name"

Idea: The idea is to create something that is portlet specific.
Lets create the service builder and since in the <entity> we do have attribute as data-source, so this may help.
To specify the data-source's value we'll use ext-spring.xml..but HOW????

What to do?
1. Create a portlet (http://michi-path.blogspot.in/2012/03/create-new-portlet-in-liferay-ide.html).
2. create service-builder.
3. In Service.xml make the entry
    <entity name="User" table="myUserTable" local-service="true" remote-service="true" data-source="eportalDB">
        <!-- PK fields -->
        <column name="user_id" type="long" primary="true" />
        <!-- Audit fields -->
        <column name="name" type="String" />
     </entity>

 4. Do "ant build-service"
5. Go to docroot/WEB-INF/src/META-INF
create a file ext-spring.xml
6. add the following code in ext-spring.xml:
<?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="eportalDB" lazy-init="true"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/myDB?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="eportalDB" />
    </bean>
    <bean id="liferayTransactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="dataSource" ref="eportalDB" />
         <property name="globalRollbackOnParticipationFailure" value="false" />
         <property name="sessionFactory" ref="liferayHibernateSessionFactory" />
    </bean>
    <bean id="liferaySessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
         <property name="sessionFactoryImplementor" ref="liferayHibernateSessionFactory" />
    </bean>
</beans>

7. build-service and deploy

Discussion: In ext-spring.xml
liferayTransactionManager:- is for the DML operations.
if this is not added we wont be able to do operations like insert, update, delete

For only fetching the data from the table, the following code can be removed from the existing ext-spring.xml
<bean id="liferayTransactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="dataSource" ref="eportalDB" />
         <property name="globalRollbackOnParticipationFailure" value="false" />
         <property name="sessionFactory" ref="liferayHibernateSessionFactory" />
    </bean>
    <bean id="liferaySessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
         <property name="sessionFactoryImplementor" ref="liferayHibernateSessionFactory" />
    </bean>


No comments:

Post a Comment