« Back to Development

Build Services Against External Database

(Redirected from Connecting to an External Database and Building Services Against It)

Introduction#

This is a step-by-step guide detailing how to connect to an external database in addition to the default "lportal" Liferay schema/database, and then generate services against that datasource using Liferay Service Builder. This is for Liferay 5.2.

portal-ext.properties #

    #
    # MySQL
    #
    jdbc.default.driverClassName=com.mysql.jdbc.Driver
    jdbc.default.url=jdbc:mysql://localhost/lportal524?emulateLocators=true&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
    jdbc.default.username=root
    jdbc.default.password=asdf
   
    jdbc.test.driverClassName=com.mysql.jdbc.Driver
    jdbc.test.url=jdbc:mysql://localhost/test?emulateLocators=true&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
    jdbc.test.username=root
    jdbc.test.password=asdf

Above I added another Mysql DB. You can change it to whatever other DB vendor. The point is, there are two.

infrastructure-spring-ext.xml#

<?xml version="1.0" encoding="UTF-8"?>

<beans
    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-2.5.xsd"
>
    <bean id="testDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.test." />
            </bean>
        </property>
    </bean>   
    <bean id="testHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration" lazy-init="true">
        <property name="dataSource">
            <ref bean="testDataSource" />
        </property>
    </bean>
    <bean id="testSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
        <property name="sessionFactoryImplementor">
            <ref bean="testHibernateSessionFactory" />
        </property>
    </bean>
    <bean id="testTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
        <property name="dataSource">
            <ref bean="testDataSource" />
        </property>
        <property name="sessionFactory">
            <ref bean="testHibernateSessionFactory" />
        </property>
    </bean>

</beans>

I called mine, "test". Also notice the "jdbc.test."

portal-ext.properties#

##
## Spring
##

    #
    # Input a list of comma delimited Spring configurations. These will be
    # loaded after the bean definitions specified in the contextConfigLocation
    # parameter in web.xml.
    #
    spring.configs=\
        META-INF/base-spring.xml,\
        \
        META-INF/hibernate-spring.xml,\
        META-INF/infrastructure-spring.xml,\
        META-INF/management-spring.xml,\
        \
        META-INF/util-spring.xml,\
        \
        META-INF/editor-spring.xml,\
        META-INF/jcr-spring.xml,\
        META-INF/messaging-spring.xml,\
        META-INF/scheduler-spring.xml,\
        META-INF/search-spring.xml,\
        \
        META-INF/counter-spring.xml,\
        META-INF/document-library-spring.xml,\
        META-INF/lock-spring.xml,\
        META-INF/mail-spring.xml,\
        META-INF/portal-spring.xml,\
        META-INF/portlet-container-spring.xml,\
        META-INF/wsrp-spring.xml,\
        \
        #META-INF/dynamic-data-source-spring.xml,\
        #META-INF/shard-data-source-spring.xml,\
        \
        META-INF/ext-spring.xml,\
        META-INF/infrastructure-spring-ext.xml

Added the last line to take infrastructure-spring-ext.xml into effect.

Ant deploy the EXT SDK. Start up the Tomcat server to make sure there are no errors in the log.#

service.xml#

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd">

<service-builder package-path="org.test">
    <author>Tester</author>
    <namespace>Test</namespace>
    <entity name="test" table="foo" local-service="true" remote-service="false" data-source="testDataSource" session-factory="testSessionFactory" tx-manager="testTransactionManager">

        <!-- PK fields -->

        <column name="fooId" type="long" primary="true" />

        <!-- Other fields -->

        <column name="name" type="String" />

    </entity>
    <exceptions>
    </exceptions>
</service-builder>

A very simple service.xml to an already existing DB table called "foo"

build-parent.xml in ext-impl.#

    <target name="build-service-test">
        <antcall target="build-service">
            <param name="service.file" value="src/org/test/service.xml" />
        </antcall>
    </target>

Add the above as an ant task. It points to the location of the service.xml

Ant build-service-test from ext-impl directory. Make sure the build is "successful".#

Go to ext-impl/src/org/test/service/impl/testLocalServiceImpl.java (might differ according to your package)#

package org.test.service.impl;

import java.util.List;

import org.test.model.test;
import org.test.service.base.testLocalServiceBaseImpl;
import org.test.service.persistence.testUtil;


import com.liferay.portal.SystemException;

public class testLocalServiceImpl extends testLocalServiceBaseImpl {
       
    @SuppressWarnings("unchecked")
    public List findAll()
        throws SystemException {
   
        List<test> list =
            (List<test>) testUtil.findAll();
        return list;
    }
   

}

Add a simple method as shown above for something like findAll().

"ant build-service-test" again. Make sure there are no errors.#

"ant deploy" from EXT SDK (top level)#

start Tomcat, look for errors.#

PluginsSDK. Go to portlets directory. Type: create.bat test "Test"#

cd into the test-portlet directory that was created and "ant deploy". Monitor log to ensure portlet is deployed.#

Make a dummy class in the portlet, call it testPortlet.java.#

For example:

package org.test;

import java.util.List;
import org.test.model.test;
import com.liferay.portal.SystemException;
import org.test.service.testLocalServiceUtil;

public class testPortlet {

    public static List findAll()
        throws SystemException {
   
        List<test> list =
            (List<test>) testLocalServiceUtil.findAll();
        return list;
    }
   
}

The testLocalServiceUtil is the wrapper class that is called. We are calling the findAll() method we implemented earlier. We didn't have to make this dummy class, but it's just to demonstrate how to call the Util from Java.

Change JSPPortlet class that was generated in the portlet code to test the method.#

Go to the doView method:

    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {

        List<test> a = null;
        try {
            a = testPortlet.findAll();
        } catch (SystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("--> Items in test table: "+a.size());
       
        include(viewJSP, renderRequest, renderResponse);
    }

It's very simple code that will output in the log. Obviously, you can do something more fancy, or from the JSP.

"ant deploy" from the test-portlet project directory. Make sure it compiles/deploys (check log).#

Place the portlet on a new test page and if it displays, check the log for the output.#

It should have pulled the data and given you the count.

0 Attachments
26527 Views
Average (3 Votes)
The average rating is 3.0 stars out of 5.
Comments
Threaded Replies Author Date
hi, is it possible to modified... delang j January 22, 2011 1:28 PM
sorry, just found the answer here... delang j January 22, 2011 1:32 PM

hi,
is it possible to modified infrastructure-spring.xml inside plugins to connect another datasource?

thanks.
Posted on 1/22/11 1:28 PM.
sorry, just found the answer here http://www.liferay.com/community/wiki/-/wiki/Main/Connect+to+a+Database+with+Plu­gins+SDK

gonna give it a try
thanks.
Posted on 1/22/11 1:32 PM in reply to delang j.