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”>
<service-builder package-path=”com.aonhewitt.benit.gmc.services”>
<author>Rtbaird</author>
<namespace>mynamespace</namespace>
<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” />
</order>
<finder name=”MailboxId” return-type=”Collection” >
<finder-column name=”mailboxid” />
</finder>
</entity>
</service-builder>
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.
<bean>
<property name=”location”>
<value>/WEB-INF/jdbc.properties</value>
</property>
<property name=”placeholderPrefix” value=”${jdbc.” />
</bean>
<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>
<bean id=”liferayHibernateSessionFactory” class=”com.liferay.portal.spring.hibernate.PortletHibernateConfiguration”>
<property name=”dataSource” ref=”zzzDataSource” />
</bean>
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):
xxx.driverClassName=com.ibm.db2.jcc.DB2Driver
xxx.url=jdbc:db2:///SCHEMA
xxx.username=username
xxx.password=password
xxx.defaultAutoCommit=false
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.
Hi.
The instructions seemed pretty straightforward as I’ve been trying variations of “How to Connect To An External Database” for 2 weeks — btw using Liferay 6.x
It’s insane that Liferay would assume that an enterprise app would use there data store; more likely than not, an enterprise will adopt the tool but have legacy data.
Anyway, I copied and pasted the configurations and only changed the data source name and the result is:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘inkwellDataSource’ is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1089)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
I keep thinking it has to be something simple (when I find it).
Can I buy a vowel please?
Thanks In Advance
BWC
Did you add a inkwellDataSource bean (case sensitive) in your WEB-INF/classes/META-INF/ext-spring.xml file (which you need to create yourself)? Check your bean id tag and your datasource property ref to make sure they all match.
Maybe it’s the XML. In any event, it looks just like the example above substituting ‘zzzDataSource’ with ‘inkwellDataSource’.
Thanks again
Regards
bwc
Ok.
I use eclipse, so I did a case-sensitive search for inkwellDataSource. Found the references in expected files.
Here’s the contents:
/WEB-INF/jdbc.properties
Sorry to clutter this post, but here’s the top of the stack trace. I tried Googling but nothing out there was helpful.
Loading file:/C:/tools/liferay-portal-tomcat-6.0.5/tomcat-6.0.26/webapps/contact-management-portlet/WEB-INF/classes/service.properties
21:09:10,442 ERROR [PortletApplicationContext:80] org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unnamed bean definition specifies neither ‘class’ nor ‘parent’ nor ‘factory-bean’ – can’t generate bean name
Offending resource: ServletContext resource [/WEB-INF/classes/META-INF/ext-spring.xml]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unnamed bean definition specifies neither ‘class’ nor ‘parent’ nor ‘factory-bean’ – can’t generate bean name
Offending resource: ServletContext resource [/WEB-INF/classes/META-INF/ext-spring.xml]
All subsequent errors has this as the root.
Hope this is enough info.
Regards.
bwc
For some reason, the contents didn’t post. Here it is:
/WEB-INF/jdbc.properties
Regards,
bwc
hi
I followed the same procedure but getting the same error
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unnamed bean definition specifies neither ‘class’ nor ‘parent’ nor ‘factory-bean’ – can’t generate bean name
10:02:51,587 ERROR [PortletApplicationContext:80] org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unnamed bean definition specifies neither ‘class’ nor ‘parent’ nor ‘factory-bean’ – can’t generate bean name
Offending resource: ServletContext resource [/WEB-INF/classes/META-INF/ext-spring.xml]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unnamed bean definition specifies neither ‘class’ nor ‘parent’ nor ‘factory-bean’ – can’t generate bean name
Offending resource: ServletContext resource [/WEB-INF/classes/META-INF/ext-spring.xml]
annot resolve reference to bean ‘zzzDataSource’ while setting bean property ‘dataSource’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘zzzDataSource’ defined in ServletContext resource [/WEB-INF/classes/META-INF/ext-spring.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
finally iam getting this error
Please help me out
Unfortunately, It is not working in my side. Is there anyone could use this solution?
This is overall consider as shit blog, noOne get successful by following these rotten procedure……….. bob before through shit in a blog you should follow the steps yourself .