Liferay 7 External Database using JNDI

Liferay 7 External Database using JNDI

In this tutorial we will see how to integrate with external database using JNDI with Service Builder.


  • Liferay Eclipse IDE M1
  • Liferay 7 Tomcat GA 3
  • Create new database scheme external and create table “country” with columns name and id

Liferay 7 External Database using JNDI tutorial will guide on creating service builder layer for external database  by using JNDI resource.  Lets create service builder project and configure JNDI datasource. Click on below tutorial to access configuring datasource via spring bean.

Liferay 7 External Database ServiceBuilder Integration

Liferay 7 External Database ServiceBuilder Integration In this tutorial, we will see how to configure external database via service builder. If your application wants to communicate to external database to load the data into liferay system. you can follow below…


Liferay 7 External Database Steps:

  1. Create Service Builder Project in Liferay 7 IDELiferay 7 Exteranl Database tutorial
  2. Give the package and leave component class Name  empty Class info
  3. Configure JNDI Resource entry in server.xml like below in tomcat/conf
    1. <GlobalNamingResources>
  4. Create Resource link in context.xml file in tomcat/conf
    1. <Context>
       <ResourceLink name="jdbc/externalDataSource" global="jdbc/externalDataSource" type="javax.sql.DataSource"/>
  5. edit service.xml file like below. In the entity level add data-source attribute with “extDataSource“. We will create spring bean “extDataSource” that will point to JNDI datasource
    • data-source attribute to custom data source.. let’s say “extDataSource”
    • table as Country
    • at column level, update db-name value with corresponding column name
    • <service-builder package-path="org.javasavvy.demo" >
       <entity local-service="true" name="Country" table="country" data-source="extDataSource" 
       remote-service="false" uuid="false">
       <column name="countryId" db-name="countryId" primary="true" type="long" />
       <column name="countryName" db-name="countryName" type="String" />
  6. Create ext-spring.xml file in META-INF/spring directory.  This file is to configure override existing liferayDataSource
    • jndiDataSource bean configure JNDI name and make sure that propertyPrefix value should be prefixed with  In this case, I used “custom.”  so properties should have each key prefixed with “custom.” only
    • do not change liferayDataSource bean Id name as it is resource by hibnernateSessionFactory and others. If you change the name then modify datasource config in hibernateSessionFactory.
    • <?xml version="1.0" encoding="UTF-8"?>
      <beans default-destroy-method="destroy" default-init-method="afterPropertiesSet"
       xmlns="" xmlns:xsi=""
       <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean" id="jndiDatasource">
            <property name="propertyPrefix" value="custom." />
           <property name="properties">
                     <prop key="">jdbc/externalDataSource</prop>
       <bean  class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" id="liferayDataSource">
            <property name="targetDataSource" ref="jndiDatasource" />
       <alias alias="extDataSource" name="liferayDataSource" />
  7. Now  execute buildService gradle task and deploy the external-jdbc modules.



Download Liferay 7 External data with JNDI in Service Builder


2 thoughts on “Liferay 7 External Database using JNDI”

Leave a Reply

Your email address will not be published. Required fields are marked *