Posts Tagged ‘service builder’

If you are looking to create a portlet that accesses a non-liferay database, follow these instructions. I am using the Liferay 6.0.x Tomcat bundle with the eclipse ide plugin to create a portlet that accesses a db2 database. The main lportal database is living in mysql.

My service.xml file looks like this:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE service-builder PUBLIC “-//Liferay//DTD Service Builder 6.0.0//EN” “http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd”&gt;
<service-builder package-path=”com.aonhewitt.benit.gmc.services”>
<entity name=”MailboxItem” local-service=”true” remote-service=”false” data-source=”zzzDataSource” table=”mailboxitem”>
<column name=”mailboxitemid” type=”String” primary=”true” />
<column name=”mailboxid” type=”String” />
<column name=”publicationdate” type=”Date” />
<column name=”docdesc” type=”String” />
<order by=”asc”>
<order-column name=”publicationdate” />
<finder name=”MailboxId” return-type=”Collection” >
<finder-column name=”mailboxid” />

The key here is the data-source attribute on the entity. I will need to define this datasource in spring files. Once you run the service builder, take a look at the service.properties file generated in your source directory. You’ll see references to all the spring-configs files which includes an extension file listed at the end for WEB-INF/classes/META-INF/ext-spring.xml. This is where you will put your spring file overrides. You’ll need to define your data source here as well as override the hibernate session bean.

<property name=”location”>
<property name=”placeholderPrefix” value=”${jdbc.” />
<bean id=”zzzDataSource”>
<property name=”driverClassName” value=”${jdbc.xxx.driverClassName}” />
<property name=”url” value=”${jdbc.xxx.url}” />
<property name=”username” value=”${jdbc.xxx.username}” />
<property name=”password” value=”${jdbc.xxx.password}” />
<bean id=”liferayHibernateSessionFactory” class=”com.liferay.portal.spring.hibernate.PortletHibernateConfiguration”>
<property name=”dataSource” ref=”zzzDataSource” />

Here I’ve defined a property placeholder which allows you to specify the jdbc properties in a file called jdbc.properties. This file should be created in the WEB-INF directory and look like this. Notice “jdbc” was left off (since the property placeholder defined the prefix):


Now you’ve defined the datasource, defined the database connection properties, and overrode the hibernateSessionFactory to use this data source (main session factory definition is in the hibernate-spring.xml file).

So to recap:

  • service.xml defines your service and what datasource to use for a given entity/table
  • [run service builder]
  • service.properties defines your spring override file – you shouldn’t need to change this
  • ext-spring.xml (you need to create) defines the spring overrides including the hibernateSessionFactory, datasource bean, and property placeholder bean
  • jdbc.properties defines the database connection properties

After publishing my portlet to Tomcat, it is pulling data from an external DB2 database. ¬†Notice I’ve said nothing about creating a datasource inside of Tomcat itself for this database connection. ¬†As surprising as that is, it doesn’t seem to be needed.


Read Full Post »