Liferay Service Builder – Connect to External/Seperate Database

  • Create a  Liferay plugin project
  • Create a service.xml , ex like below.


<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE service-builder PUBLIC “-//Liferay//DTD Service Builder 6.2.0//EN” “http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd”>
<service-builder package-path=”com.dinacs” auto-namespace-tables=”false”>
<author>vishnu</author>
<namespace>dinacs</namespace>
<entity name=”Feedback” local-service=”true” remote-service=”true”>

<column name=”feedbackId” type=”long” primary=”true” />

<column name=”feedbackTitle” type=”String” />
<column name=”feedbackText” type=”String” />
</entity>
</service-builder>

  • Build the service
  • Add your separate database details in portal-ext.properties ex like below.

# liferay default db
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

# seperate database
jdbc.dinacs.driverClassName=com.mysql.jdbc.Driver
jdbc.dinacs.url=jdbc:mysql://localhost/dinacsdb?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.dinacs.username=root
jdbc.dinacs.password=root

  • Create ext-spring.xml file under docroot/WEB-INF/src/META-INF
  • Place the content like below in ext-spring.xml

<?xml version=”1.0″?>

<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:context=”http://www.springframework.org/schema/context”
xmlns:tx=”http://www.springframework.org/schema/tx” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”>

<bean id=”basePersistence” abstract=”true”>
<property name=”dataSource” ref=”dinacsDS” />
<property name=”sessionFactory” ref=”dinacsSessionFactory” />
</bean>

<bean id=”dinacsHibernateSessionFactory”
class=”com.liferay.portal.spring.hibernate.PortletHibernateConfiguration”
lazy-init=”true”>
<property name=”dataSource” ref=”dinacsDS” />
</bean>

<bean id=”dinacsSessionFactory” class=”com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl”
lazy-init=”true”>
<property name=”sessionFactoryImplementor” ref=”dinacsHibernateSessionFactory” />
</bean>

<bean id=”dinacsDS”
class=”org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy”>
<property name=”targetDataSource”>
<bean class=”com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean”>
<property name=”propertyPrefix” value=”jdbc.dinacs.” />
</bean>
</property>
</bean>
</beans>

  • Edit the service.xml and add data-source , session-factory and tx-manager attributes to the entity tag.
  • Ex like below.

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE service-builder PUBLIC “-//Liferay//DTD Service Builder 6.2.0//EN” “http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd”>
<service-builder package-path=”com.dinacs” auto-namespace-tables=”false”>
<author>vishnu</author>
<namespace>dinacs</namespace>
<entity name=”Feedback” local-service=”true” remote-service=”true” data-source=”dinacsDS” session-factory=”dinacsSessionFactory” tx-manager=”dinacsTransactionManager” >

<column name=”feedbackId” type=”long” primary=”true” />

<column name=”feedbackTitle” type=”String” />
<column name=”feedbackText” type=”String” />
</entity>
</service-builder>

  • Build the service again.
  • Deploy the portlet.


Note : tables will be created in the liferay default database only , need to manually create the tables in separate database.