« Back to Development

Connect to a Database with Plugins SDK

Tags: liferay v5.2

Introduction #

This is possible because Liferay architecture allows it in its Plugins SDK.

Environment #

  • Lifery-tomcat-6.0-5.2.5 Plugins SDK
  • MySQL

Configuration to be Updated #

Create a portlet from the sample #

Include the following in conf/context.xml #

	<Resource
		name="jdbc/TrainingPool"
		auth="Container"
		type="javax.sql.DataSource"
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost/training?useUnicode=true&amp;characterEncoding=UTF-8"
		username="lportal"
		password="lportal"
		maxActive="20"
	/>

Write service.xml as follows and build service #

<?xml version="1.0" encoding="UTF-8"?>
<!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="com.bc.od.store">
	<author>Your Name</author>
	<namespace>OD</namespace>
	<entity name="Training" local-service="true" data-source="trainingDataSource" session-factory="trainingSessionFactory" tx-manager="trainingTransactionManager">
		<column name="userId" type="long" primary="true"></column>
		<column name="dogName" type="String"></column>
		<column name="wifeName" type="String"></column>
	</entity>
</service-builder>

Create database and run queries #

create database training character set utf8;

grant all on training.* to 'lportal'@'localhost' identified by 'lportal' with grant option;
grant all on training.* to 'lportal'@'localhost.localdomain' identified by 'lportal' with grant option;

CREATE TABLE `training` (
  `userId` bigint(20) NOT NULL DEFAULT '0',
  `dogName` varchar(75) DEFAULT NULL,
  `wifeName` varchar(75) DEFAULT NULL,
  PRIMARY KEY  (`userId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

insert into training.training values (1,"ada","betsy");
insert into training.training values (2,"cynthia","dorothy");

Update configuration files with listed content #

base-spring.xml:

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

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
	<bean id="beanReferenceAnnotationBeanPostProcessor" class="com.liferay.portal.spring.annotation.BeanReferenceAnnotationBeanPostProcessor" />
	<aop:config>
		<aop:pointcut id="transactionOperation" expression="bean(*Service.impl)" />
		<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionOperation" />
	</aop:config>

	<bean id="transactionAdvice" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="trainingTransactionManager" />
		<property name="transactionAttributeSource">
			<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource">
				<constructor-arg>
					<bean class="com.liferay.portal.spring.annotation.PortalTransactionAnnotationParser" />
				</constructor-arg>
			</bean>
		</property>
	</bean>
	
	<bean id="portletBeanFactoryPostProcessor" class="com.liferay.portal.spring.context.PortletBeanFactoryPostProcessor" />
	<bean id="portletClassLoader" class="com.liferay.portal.kernel.portlet.PortletClassLoaderUtil" factory-method="getClassLoader" />
	<bean id="logAdvice" class="com.liferay.portal.spring.aop.LogAdvice" />
	<bean id="velocityServiceInterceptor" class="com.liferay.portal.spring.aop.BeanInterceptor">
		<property name="classLoader" ref="portletClassLoader" />
		<property name="exceptionSafe" value="true" />
	</bean>
	<bean id="baseVelocityService" abstract="true">
		<property name="interceptorNames">
			<list>
				<value>velocityServiceInterceptor</value>
			</list>
		</property>
	</bean>
	<bean id="baseModelExtensionAdvice" class="com.liferay.portal.spring.aop.ModelExtensionAdvice" abstract="true" />
	
	<bean id="basePersistence" abstract="true">
		<property name="dataSource" ref="trainingDataSource" />
		<property name="sessionFactory" ref="trainingSessionFactory" />
	</bean>
	
</beans>

hibernate-spring.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="trainingHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration" lazy-init="true">
		<property name="dataSource">
			<ref bean="trainingDataSource" />
		</property>
		<property name="mappingResources">
            <list>
                <value>META-INF/portlet-hbm.xml</value>
            </list>
		</property>
	</bean>
	<bean id="trainingSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
		<property name="sessionFactoryImplementor">
			<ref bean="trainingHibernateSessionFactory" />
		</property>
	</bean>
	<bean id="trainingTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
		<property name="dataSource">
			<ref bean="trainingDataSource" />
		</property>
		<property name="sessionFactory">
			<ref bean="trainingHibernateSessionFactory" />
		</property>
	</bean>
</beans>

infrastructure-spring.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="trainingDataSourceTarget" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
		<property name="jndiName">
			<value>jdbc/TrainingPool</value>
		</property>
	</bean>
	<bean id="trainingDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
		<property name="targetDataSource">
			<ref bean="trainingDataSourceTarget" />
		</property>
	</bean>
</beans>

Deploy the portlet and test #

Add code in view.jsp as follows:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

This is the <b>Sample JSP Portlet</b>. Use this as a quick way to include JSPs.

<%
java.util.List<com.bc.od.store.model.Training> trainings = null;
com.bc.od.store.model.Training aTrain = null;

try{
	trainings = com.bc.od.store.service.TrainingLocalServiceUtil.getTrainings(0, 2);
	
	aTrain = com.bc.od.store.service.TrainingLocalServiceUtil.getTraining(1);
	
}
catch(Exception e){
	e.printStackTrace();
}
if(trainings != null){
	System.out.println("trainings are not null");
	for(com.bc.od.store.model.Training t: trainings){
		System.out.println("training: " + t.getWifeName());
	}
}

if(aTrain != null){
	System.out.println("train is not null: " + aTrain.getWifeName());
}
%>

Future Improvements #

(1) Try using CounterUtil.java to get primary keys for tables in the separate database.

(2) The fields of the separate database table are not indexed. So Lucene search may not work.

(3) Tables are not automatically created in the separate database.

0 Attachments
34994 Views
Average (1 Vote)
The average rating is 5.0 stars out of 5.
Comments
Threaded Replies Author Date
Hi, Thank you. Can you attached the source code? Jimmy Chen November 3, 2010 10:52 PM
Hi, I followed the steps and tried it on... Jimmy Chen November 4, 2010 1:29 AM
Hi, Jimmy, There are various causes for... Xinsheng Robert Chen December 7, 2010 9:12 AM
i thought that db conn made in... delang j January 22, 2011 1:36 PM
I tried this.but get following error. Can... vaibhav khopade February 16, 2011 7:50 AM
Hi!! Thank you for the guide, it's really... Borja Yrazu April 11, 2011 8:29 AM
Hi. This guide is very helpful. Is there an... Vic Kumar May 18, 2011 7:09 PM
Hi I am getting the following error... Mohammed Azam May 23, 2011 7:06 AM
I am trying to used MySQL as liferay and Oracle... Vince Yoon August 24, 2011 5:11 PM
please have a look at this link... Mohammed Azam September 24, 2011 3:49 AM
este link não funciona mais. ricardo wolosker January 7, 2012 2:44 PM
There is the real link ... Paul Owner March 22, 2012 8:55 AM
The link is not paste well, just go to... Paul Owner March 22, 2012 8:58 AM
Has this actually worked for anyone? Richard Tichy September 6, 2012 4:51 AM
Will the changes to the various xml files not... Richard Tichy September 6, 2012 4:58 AM
I am using above code i am getting error.... anand gopalan October 11, 2012 6:21 AM

Hi,
Thank you. Can you attached the source code?
Posted on 11/3/10 10:52 PM.
Hi,

I followed the steps and tried it on Liferay 6, it does not work. Always get Context initialization failed error.
Posted on 11/4/10 1:29 AM.
Hi, Jimmy,

There are various causes for context initialization failures.

The error message may be as follows:

21:45:14,166 INFO [PortletHotDeployListener:379] 1 portlet for xxxxxx-
portlet is available for use
Dec 2, 2010 9:45:14 PM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
Dec 2, 2010 9:45:14 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/xxxxxx-portlet] startup failed due to previous errors

The causes may be as follows:

(1) The required jar files have not been included -- the context initializer listener cannot find the class definition. It gives up.
(2) When the listener tries to initialize a data source object, the data source jndi name is wrong. It gives up.
(3) The required class has not been compiled. The listener cannot find the definition of the class -- Maven may deploy a package without compiling all the classes.
Posted on 12/7/10 9:12 AM in reply to Jimmy Chen.
i thought that db conn made in portal-ext.properties.
Posted on 1/22/11 1:36 PM.
I tried this.but get following error. Can someone help.
15:47:33,680 INFO [PortletHotDeployListener:435] 1 portlet for Vaibhav-portlet was unregistered
Loading file:/C:/Liferay-6.0.5/liferay-portal-6.0.5/tomcat-6.0.26/webapps/Vaibhav-portle­t/WEB-INF/classes/service.properties
Loading file:/C:/Liferay-6.0.5/liferay-portal-6.0.5/tomcat-6.0.26/webapps/Vaibhav-portle­t/WEB-INF/classes/service.properties
15:47:34,255 ERROR [ContextLoader:220] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Ca
nnot resolve reference to bean 'asyncAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error crea
ting bean with name 'asyncAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'threadLocalCacheAdvice' while setting
bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'threadLocalCacheAdvice' defined in Se
rvletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'transactionAdvice' while setting bean property 'nextMethodInterceptor'; nested excep
tion is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-
data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.fac
tory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstanti­ateSingletons(DefaultListableBeanFactory.java:575)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactory­Initialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractA­pplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(Contex­tLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextL­oader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(Context­LoaderListener.java:47)
at com.liferay.portal.spring.context.PortletContextLoaderListener.contextInitialize­d(PortletContextLoaderListener.java:71)
at com.liferay.portal.kernel.servlet.PortalClassLoaderServletContextListener.doPort­alInit(PortalClassLoaderServletContextListener.java:91)
at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycl­e.java:42)
at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.­java:52)
at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BaseP­ortalLifecycle.java:50)
at com.liferay.portal.kernel.servlet.PortalClassLoaderServletContextListener.contex­tInitialized(PortalClassLoaderServletContextListener.java:52)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1244)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1342)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:119)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337­)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe­rBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'asyncAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring
.xml]: Cannot resolve reference to bean 'threadLocalCacheAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationE
xception: Error creating bean with name 'threadLocalCacheAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'transa
ctionAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transaction
Advice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean pr
operty 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 32 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'threadLocalCacheAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/
base-spring.xml]: Cannot resolve reference to bean 'transactionAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCre
ationException: Error creating bean with name 'transactionAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-data-source-spring.xml]: Cannot resolve reference
to bean 'liferayTransactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 42 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynam
ic-data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.
factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 52 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefi­nition(DefaultListableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBean­Definition(AbstractBeanFactory.java:1089)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 62 more
15:47:34,371 ERROR [PortalClassLoaderServletContextListener:94] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceAdvice' defined in Servlet
Context resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'asyncAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org
.springframework.beans.factory.BeanCreationException: Error creating bean with name 'asyncAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot
resolve reference to bean 'threadLocalCacheAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Erro
r creating bean with name 'threadLocalCacheAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'transactionAdvice' w
hile setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionAdvice' define
d in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean property 'transa
ctionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Ca
nnot resolve reference to bean 'asyncAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error crea
ting bean with name 'asyncAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'threadLocalCacheAdvice' while setting
bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'threadLocalCacheAdvice' defined in Se
rvletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'transactionAdvice' while setting bean property 'nextMethodInterceptor'; nested excep
tion is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-
data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.fac
tory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstanti­ateSingletons(DefaultListableBeanFactory.java:575)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactory­Initialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractA­pplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(Contex­tLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextL­oader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(Context­LoaderListener.java:47)
at com.liferay.portal.spring.context.PortletContextLoaderListener.contextInitialize­d(PortletContextLoaderListener.java:71)
at com.liferay.portal.kernel.servlet.PortalClassLoaderServletContextListener.doPort­alInit(PortalClassLoaderServletContextListener.java:91)
at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycl­e.java:42)
at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.­java:52)
at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BaseP­ortalLifecycle.java:50)
at com.liferay.portal.kernel.servlet.PortalClassLoaderServletContextListener.contex­tInitialized(PortalClassLoaderServletContextListener.java:52)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1244)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1342)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:119)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337­)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe­rBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'asyncAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring
.xml]: Cannot resolve reference to bean 'threadLocalCacheAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationE
xception: Error creating bean with name 'threadLocalCacheAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'transa
ctionAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transaction
Advice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean pr
operty 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 32 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'threadLocalCacheAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/
base-spring.xml]: Cannot resolve reference to bean 'transactionAdvice' while setting bean property 'nextMethodInterceptor'; nested exception is org.springframework.beans.factory.BeanCre
ationException: Error creating bean with name 'transactionAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynamic-data-source-spring.xml]: Cannot resolve reference
to bean 'liferayTransactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 42 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionAdvice' defined in ServletContext resource [/WEB-INF/classes/META-INF/dynam
ic-data-source-spring.xml]: Cannot resolve reference to bean 'liferayTransactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.
factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app­lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.pop­ulateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doC­reateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cre­ateBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(Abstra­ctBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingle­ton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 52 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'liferayTransactionManager' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefi­nition(DefaultListableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBean­Definition(AbstractBeanFactory.java:1089)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 62 more
Loading file:/C:/Liferay-6.0.5/liferay-portal-6.0.5/tomcat-6.0.26/webapps/Vaibhav-portle­t/WEB-INF/classes/service.properties
15:47:34,733 INFO [PortletHotDeployListener:220] Registering portlets for Vaibhav-portlet
15:47:34,994 INFO [PortletHotDeployListener:369] 1 portlet for Vaibhav-portlet is available for use
Posted on 2/16/11 7:50 AM.
Hi!! Thank you for the guide, it's really useful!

I´ve got a problem launching the portlet when using TrainingLocalServiceUtil.getTrainings: "Table 'training.od_training' doesn't exist". This 'od' is because of the environment in service.xml

Any idea to fix it?

Thanks again!!
Posted on 4/11/11 8:29 AM in reply to vaibhav khopade.
Hi. This guide is very helpful. Is there an update for Liferay 6? Would we use different DTD's in the xml documents? Also, I am fairly new to Liferay and wasn't sure about the folder structure. Do all these xml files go under the WEB-INF in my docroot? Thanks in advance.
Posted on 5/18/11 7:09 PM in reply to Borja Yrazu.
Hi

I am getting the following error
BeanLocator is null
Posted on 5/23/11 7:06 AM.
I am trying to used MySQL as liferay and Oracle for an application(i.e. a portlet). Problem is .... MySQL SQL statements are generated for Oracle database when actually SQL statements are executed. I guess because MySql is default one. Is there any solution to use two different dialects?
Posted on 8/24/11 5:11 PM.
please have a look at this link http://liferayazam.wordpress.com/2011/05/30/connect-to-different-database-throug­h-service-xml-liferay6/ .. I have got it working
Posted on 9/24/11 3:49 AM in reply to Vince Yoon.
este link não funciona mais.
Posted on 1/7/12 2:44 PM in reply to Mohammed Azam.
There is the real link

http://liferayazam.wordpress.com/2011/05/30/connect-to-different-database-th­rough-service-xml-liferay6/
Posted on 3/22/12 8:55 AM in reply to ricardo wolosker.
The link is not paste well, just go to http://liferayazam.wordpress.com/ and click in
"Connect to different database through service.xml. Liferay6"
Posted on 3/22/12 8:58 AM in reply to Paul Owner.
Has this actually worked for anyone?
Posted on 9/6/12 4:51 AM in reply to Paul Owner.
Will the changes to the various xml files not be wiped out next time you run the service builder?
Posted on 9/6/12 4:58 AM.
I am using above code i am getting error. Please tell me how to fix this

Offending resource: ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]
Bean 'beanReferenceAnnotationBeanPostProcessor'; nested exception is java.lang.ClassNotFoundException: com.liferay.portal.spring.annotation.BeanReferenceAnnotationBeanPostProcessor
org­.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean class [com.liferay.portal.spring.annotation.BeanReferenceAnnotationBeanPostProcessor] not found
Offending resource: ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]
Bean 'beanReferenceAnnotationBeanPostProcessor'; nested exception is java.lang.ClassNotFoundException: com.liferay.portal.spring.annotation.BeanReferenceAnnotationBeanPostProcessor
at­ org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFast­ProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java­:85)
..............
..............
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: com.liferay.portal.spring.annotation.BeanReferenceAnnotationBeanPostProcessor
at­ java.lang.ClassLoader.findClass(ClassLoader.java:359)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

.......................
....­.................

13:05:01,561 ERROR [ContextLoader:227] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.liferay.portal.spring.aop.ServiceBeanAutoProxyCreator#0' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'serviceAdvice' while setting bean property 'methodInterceptor'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'serviceAdvice' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
......................
.......­.................

at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'serviceAdvice' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefi­nition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBean­Definition(AbstractBeanFactory.java:1094)
....................................
...­..................................
13:05:01,563 ERROR [BasePortalLifecycle:45] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.liferay.portal.spring.aop.ServiceBeanAutoProxyCreator#0' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'serviceAdvice' while setting bean property 'methodInterceptor'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'serviceAdvice' is defined
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.liferay.portal.spring.aop.ServiceBeanAutoProxyCreator#0' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'serviceAdvice' while setting bean property 'methodInterceptor'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'serviceAdvice' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
.....................
...............­.......

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'serviceAdvice' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefi­nition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBean­Definition(AbstractBeanFactory.java:1094)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract­BeanFactory.java:276)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe­anFactory.java:192)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:322)
... 38 more
13:05:01,564 ERROR [MainServlet:350] java.lang.IllegalStateException: Unable to initialize portal
java.lang.IllegalStateException: Unable to initialize portal
at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycl­e.java:47)

............................
..................................
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
­at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.liferay.portal.spring.aop.ServiceBeanAutoProxyCreator#0' defined in ServletContext resource [/WEB-INF/classes/META-INF/base-spring.xml]: Cannot resolve reference to bean 'serviceAdvice' while setting bean property 'methodInterceptor'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'serviceAdvice' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveRef­erence(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveVal­ueIfNecessary(BeanDefinitionValueResolver.java:106)
Posted on 10/11/12 6:21 AM in reply to Richard Tichy.