Liferay Plugin portlet connecting to multiple data bases/Data Sources
Lifray
portal 6.1GA2+Tomcat7.X+Mysql 5.1X
Download Portlet from following URL
Place the portlet into your plugins/portlet directory
Create portlet using Liferay IDE from existing
portlet and select portlet what you have downloaded.
Note:
before import please delete .project, .classpath and .settings files from downloaded portlet.
before import please delete .project, .classpath and .settings files from downloaded portlet.
Create data
base in mysql database name is “anotherdatabase”
Create Table name is TableFromAnotherDataSource in “anotherdatabase” database.
CREATE
TABLE `tablefromanotherdatasource` (
`IFADSId` BIGINT(20) NOT NULL,
`Description` VARCHAR(75) NULL
DEFAULT NULL,
PRIMARY KEY (`IFADSId`)
);
|
Add following properties in portal-ext.properties
file
jdbc.anotherdbconfig.driverClassName=com.mysql.jdbc.Driver
jdbc.anotherdbconfig.url=jdbc:mysql://localhost:3306/anotherdatabase?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.anotherdbconfig.username=root
jdbc.anotherdbconfig.password=
|
Note:
When we change portal-ext.proprties
file you have to stop server after add some properties you have to restart.
portal-ext.properties file should be available in liferay home path if not you have to
create and add above properties.
Generally home path like below
/bundles/
portal-ext.proprties or liferay-portal-6.1.1-ce-ga2/
portal-ext.proprties
Run ant build-service target from your eclipse ant
view
Run and deploy then portle will be deployed
Note:
this portlet developed in Loferay Portal 6.1GA2 version and Plug-in SDK 6.1GA2
this portlet developed in Loferay Portal 6.1GA2 version and Plug-in SDK 6.1GA2
This portlet will use to connect to multiple data
bases. When we get requirement plugin portlet need to connect to multiple databases
or data sources we have to configure data source and Session Factory
information in ext-spring.xml.
We have to create xml file and we need to place file in src/META-INF/ext-spring.xml
We have to create xml file and we need to place file in src/META-INF/ext-spring.xml
Generally Liferay plugin portlets uses default liferay database or data source.
If we want connect to another database we have to configure those details in ext-spring.xml file.
Once we configured the data source and session factory in ext-spring.xml file then have to use these data source information in service.xml
file that’s for entity.
following is ext-spring.xml
<?xml version="1.0"?>
<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 id="anotherDataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource"
ref="anotherDataSourceWrapper" />
</bean>
<bean id="anotherDataSourceImpl"
class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
<property name="propertyPrefix"
value="jdbc.anotherdbconfig." />
</bean>
<bean id="anotherDataSourceWrapper"
class="com.liferay.portal.dao.jdbc.util.DataSourceWrapper">
<constructor-arg ref="anotherDataSourceImpl"
/>
</bean>
<bean class="com.liferay.portal.dao.jdbc.util.DataSourceSwapper">
<property name="liferayDataSourceWrapper"
ref="anotherDataSourceWrapper" />
</bean>
<bean id="anotherHibernateSessionFactory"
class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
factory-method="newBean">
<constructor-arg value="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration"
/>
<constructor-arg>
<map>
<entry key="dataSource"
value-ref="anotherDataSource" />
</map>
</constructor-arg>
</bean>
<bean id="anotherSessionFactory"
class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
factory-method="newBean">
<constructor-arg
value="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl"
/>
<constructor-arg>
<map>
<entry key="dataSource"
value-ref="anotherDataSource" />
<entry key="sessionFactoryClassLoader"
value-ref="portletClassLoader" />
<entry key="sessionFactoryImplementor"
value-ref="anotherHibernateSessionFactory" />
</map>
</constructor-arg>
</bean>
</beans>
|
Once we configured data source and
session factory in ext-spring.xml we
have to provide data source properties from portal-ext.properties file which in
you liferay home path.
Generally home path like below
/bundles/ portal-ext.proprties or liferay-portal-6.1.1-ce-ga2/ portal-ext.proprties
Add the following properties in portal-ext.proprties
jdbc.anotherdbconfig.driverClassName=com.mysql.jdbc.Driver
jdbc.anotherdbconfig.url=jdbc:mysql://localhost:3306/anotherdatabase?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.anotherdbconfig.username=root
jdbc.anotherdbconfig.password=
|
Note:
jdbc. anotherdbconfig. Is the propertyPrefix which we have mentioned in ext-spring.xml file
jdbc. anotherdbconfig. Is the propertyPrefix which we have mentioned in ext-spring.xml file
<bean id="anotherDataSourceImpl"
class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
<property name="propertyPrefix"
value="jdbc.anotherdbconfig."
/>
</bean>
|
Now we have to provide this information to entity
which we have configured in service.xml .
In entity tag we have two attributes from that we can explicitly said about data source and session factory information .
<entity name="TableFromAnotherDataSource" table="TableFromAnotherDataSource" local-service="true"
remote-service="true" data-source="anotherDataSource"
session-factory="anotherSessionFactory">
<column name="IFADSId" type="long"
primary="true" />
<column name="Description" type="String" />
</entity>
|
Note:
we have table attribute in entity tag when we mention this entity class targeting to that particular table
we have table attribute in entity tag when we mention this entity class targeting to that particular table
If we not provide table attribute then service builder
create table name as namespace_entityname
If provide table attribute then table will be created
exact name that we provided in table attribute.
Example:
We already having existed data base then we need not
create tables from service builder then we just pass table name in table
attribute then out entity class targeting to that table.
Observations:
Note:
When we use other data source for plugin portlet when we run service builder then tables creation script is not create so we have to create table manually in database if the table is new.
When we use other data source for plugin portlet when we run service builder then tables creation script is not create so we have to create table manually in database if the table is new.
If you need create table script you can see in the
class XXXModelImpl.java
XXX= entity Name
public
static final String TABLE_SQL_CREATE = "create table
TableFromAnotherDataSource (IFADSId LONG not null primary key,Description
VARCHAR(75) null)";
|
You can also find data source and session factory that
is used by your Entity class
public
static final String DATA_SOURCE = "anotherDataSource";
public
static final String SESSION_FACTORY = "anotherSessionFactory";
|
0 comments :
Post a Comment