Liferay 7 External Database using JNDI
In this tutorial we will see how to integrate with external database using JNDI with Service Builder.
Installations:
- 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:
- Create Service Builder Project in Liferay 7 IDE
- Give the package and leave component class Name empty
- Configure JNDI Resource entry in server.xml like below in tomcat/conf
-
<GlobalNamingResources> <Resource name="jdbc/externalDataSource" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/external" username="root" password="root" maxActive="20" maxIdle="5" maxWait="10000" /> </GlobalNamingResources>
-
- Create Resource link in context.xml file in tomcat/conf
-
<Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <ResourceLink name="jdbc/externalDataSource" global="jdbc/externalDataSource" type="javax.sql.DataSource"/> </Context>
-
- 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" > <namespace>jay</namespace> <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" /> </entity> </service-builder>
- 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 jndi.name. 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="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 class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean" id="jndiDatasource"> <property name="propertyPrefix" value="custom." /> <property name="properties"> <props> <prop key="custom.jndi.name">jdbc/externalDataSource</prop> </props> </property> </bean> <bean class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" id="liferayDataSource"> <property name="targetDataSource" ref="jndiDatasource" /> </bean> <alias alias="extDataSource" name="liferayDataSource" /> </beans>
- 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”
Comments are closed.
Hi, just a quick question how would this approach be different for multiple external connections?