Monday, September 24, 2012

Connect to Another Datasource/Database Schema from Portlet with service

Hi Folks,

Many developer/client's need to separate the liferay database table and custom database table. so, here is the solution to maintain this approach. for that we maintain two data source/schema for liferay and custom database. we can achieve this by trying to  following steps -

1) portal-ext.properties

Add two connection properties for maintain two differance database source/schema. for example

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

## Your new database connection
jdbc.test.driverClassName=com.mysql.jdbc.Driver
jdbc.test.url=jdbc:mysql://localhost/mytest?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.test.username=root
jdbc.test.password=root

2) ext-spring.xml

    create ext-spring.xml in docroot\WEB-INF\src\META-INF\ of you portlet to connect with other database shema default then liferay. copy the below content and paste in newly created file.
   
<<?xml version="1.0"?>
<<beans
    default-destroy-method="destroy"
    default-init-method="afterPropertiesSet"   
    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-3.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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.PortletHibernateConfiguration" lazy-init="true">
        <<property name="dataSource" ref="testDataSource" />       
    <</bean>
   

    <<bean id="testSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">       
        <<property name="sessionFactoryImplementor" ref="testHibernateSessionFactory" />
    <</bean>
   
    <<bean id="testTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
        <<property name="dataSource" ref="testDataSource" />
        <<property name="globalRollbackOnParticipationFailure" value="false"/>
        <<property name="sessionFactory" ref="testHibernateSessionFactory" />
    <</bean>

<</beans>

3) service.xml
    create entity with the parameter data-source,session-factory,tx-manager to connect other then default database. for example....
   
    <<entity name="MyTable" local-service="true" remote-service="false" data-source="testDataSource" session-factory="testSessionFactory" tx-manager="testTransactionManager">


4) service.properties

   check the entry of ext-spring.xml in \docroot\WEB-INF\src\service.properties file(some version of liferay have already this entry). if it is not there then append spring.configs properties with the value "WEB-INF/classes/META-INF/ext-spring.xml"
  
  
 At Last......

 After deploying portlet, custom portlet are able to connect with other then default database source/schema .

 NOTE :
 1)This example is  based on Liferay 6.0 and 6.0.29 tomcat bundle. you have to do specific changes in any of file(.xml) if needed based on Liferay version.
 2)While you connect with other then default liferay database source/schema. service-builder can't create automatically table in database. you have to create table manually. then after you can CURD operation for you custom table same as liferay default database.

Thursday, September 6, 2012

Liferay portal connection with various databse

Hi visitor,

here is the list of  different/various database and it's connection properties to connection Liferay portal.

 MySQL

jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=
jdbc.default.password=


 Oracle

jdbc.default.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.default.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.default.username=lportal
jdbc.default.password=lportal


DB2

jdbc.default.driverClassName=com.ibm.db2.jcc.DB2Driver
jdbc.default.url=jdbc:db2://localhost:50000/lportal:deferPrepares=false;fullyMaterializeInputStreams=true;fullyMaterializeLobData=true;progresssiveLocators=2;progressiveStreaming=2;
jdbc.default.username=db2admin
jdbc.default.password=lportal


 Derby

jdbc.default.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
jdbc.default.url=jdbc:derby:lportal
jdbc.default.username=
jdbc.default.password=


 Hypersonic

dbc.default.driverClassName=org.hsqldb.jdbcDriver
dbc.default.url=jdbc:hsqldb:${liferay.home}/data/hsql/lportal
dbc.default.username=sa
dbc.default.password=


 Ingres

jdbc.default.driverClassName=com.ingres.jdbc.IngresDriver
jdbc.default.url=jdbc:ingres://localhost:II7/lportal
jdbc.default.username=
jdbc.default.password=


 P6Spy

jdbc.default.driverClassName=com.p6spy.engine.spy.P6SpyDriver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=
jdbc.default.password=


 PostgreSQL

jdbc.default.driverClassName=org.postgresql.Driver
jdbc.default.url=jdbc:postgresql://localhost:5432/lportal
jdbc.default.username=sa
jdbc.default.password=


 SQL Server

jdbc.default.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc.default.url=jdbc:jtds:sqlserver://localhost/lportal
jdbc.default.username=sa
jdbc.default.password=


 Sybase

jdbc.default.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc.default.url=jdbc:jtds:sybase://localhost:5000/lportal
jdbc.default.username=sa
jdbc.default.password=