Pages

Monday, 11 March 2013

Connecting to different database using Liferay Service Builder


Generally we guys used to connect with the liferay default Database.
We also have requirement that Client wants there Data on Separate Schema or Separate DB.
But In Liferay how we can connect with different DB?

Don't Worry guys your problem is now solved.

Just Follow some steps as below :-

1) In Service.xml define the datasource, session-factory,transcation-manager within the Entity Definition as follows:-

<service-builder package-path="com.test.liferay">
 <namespace>test</namespace>
 <entity data-source="anotherDataSource" local-service="true" name="testDB" remote-service="false" session-factory="anotherSessionFactory" tx-manager="anotherTransactionManager">
 ......
 ......
 </entity>
</service-builder>

2) Create a new file ext-spring.xml under WEB-INF/src/META-INF dir.

 
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 <aop:config>
  <aop:pointcut expression="bean(*Service.impl)" id="transactionOperation">
  <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionOperation">
 </aop:advisor>
 <bean abstract="true" id="basePersistence">
  <property name="dataSource" ref="anotherDataSource">
  <property name="sessionFactory" ref="anotherSessionFactory">
 </property>
 <bean class="org.springframework.transaction.interceptor.TransactionInterceptor" id="transactionAdvice">
  <property name="transactionManager" ref="anotherTransactionManager">
  <property name="transactionAttributeSource">
   <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource">
    <constructor-arg>
     <bean class="com.liferay.portal.spring.annotation.PortalTransactionAnnotationParser">
    </bean>
   </constructor-arg>
  </bean>
 </property>
 <bean class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" id="tcHibernateSessionFactory" lazy-init="true"
  <property name="dataSource" ref="anotherDataSource">
 </property>
 <bean class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" id="anotherSessionFactory" lazy-init="true">
  <property name="sessionFactoryImplementor" ref="tcHibernateSessionFactory">
 </property>
 <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="anotherTransactionManager" lazy-init="true">
  <property name="dataSource" ref="anotherDataSource">
  <property name="globalRollbackOnParticipationFailure" value="false">
  <property name="sessionFactory" ref="tcHibernateSessionFactory">
 </property>
  
 <bean class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" id="anotherDataSource">
  <property name="targetDataSource">
   <bean class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
    <property name="propertyPrefix" value="jdbc.test.">
   </property>
  </bean>     
    </property>
     
</bean>
</property></property></bean></bean></bean></property></bean></property></bean></aop:pointcut></aop:config></beans>


3) And one more thing don't forget, pointing to the new DB.
    So the portal-ext.properties will look like as below :-

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

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

4) Final Step Build service with the command ant build-service.

Congratulations,Your work is done !!!!!!!!!!

No comments:

Post a Comment