Saturday, May 26, 2012

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) As you are mentioning the jdbc properties for the liferayportal in portal-ext.properties, same way mention for the external DB or seperate schema as below :-


Explanation :-
The first four line starts with the jdbc.default, it means that it was associated with the Default Data Source. So it will connect to the Default Liferay DB.

Last four line line starts with the jdbc.test, which does not belong to the Default DB. It will be connected to some other DB whose entry will be in ext-spring.xml.

2) As we are using service-builder, it means that you need new tables other than liferay default DB. So it requires for you to create new plugins project and in that you need to create service.xml under webapps/WEB-INF and with the help of ANT(ant build-service) or MAVEN (mvn liferay:build-service) you will able to create full structure for your service. But still it is pointing to the default DB.

Now you need to create a new file ext-spring.xml under WEB-INF/src/META-INF dir. Inside META-INF folder you will find couple of xml files whose entry will be there in liferay portal.properties file as below :-


If you notice the order of xml file loading in portal.properties file, then you will find that the last file is ext-spring.xml is loaded. So we will now create ext-spring.xml and putting all transaction,datasource and sessionfactory related changed on that file as below :-


3) Now the last and the final step you need to do is that you need to modify the existing service.xml as follows :-


The data-source value specifies the data source target that is set to the persistence class. The default value is the Liferay data source. This is used in conjunction with session-factory.

The session-factory value specifies the session factory that is set to the persistence class. The default value is the Liferay session factory. This is used in conjunction with data-source.

The tx-manager value specifies the transaction manager that Spring uses. The default value is the Spring Hibernate transaction manager that wraps the Liferay data source and session factory.

If the local-service value is true, then the service will generate the local interfaces for the service. The default value is false.

If the remote-service value is true, then the service will generate remote interfaces for the service. The default value is true.

You can use the local-service and remote-service attribute according to your needs.

You are now connected with your external DB.

Put your Suggestions/Comments.

Thanks for Reading !!!

11 comments:

  1. We are using different database connection. We are able to connect with this db successfully. But when we try to fetch with primary key fields like FooLocalServiceUtil.getFoo(1)); we are able to get results.
    But when we try to fetch by any other finder add by us in entity it's not giving results.

    ReplyDelete
  2. It should be, we are implemented the above code in my project and it is live now.

    Please check once again from your end each and every entry of ext-spring.xml file and let me know if you still find any issue.

    ReplyDelete
  3. Does liferay create tables in the different database which we mapped through the data-source(here testE) or to the Liferay's default database(here liferay612)?

    ReplyDelete
    Replies
    1. No. It will not create table in external schema. You need to manually create it with the sql file that was generated with the help of Service Builder

      Delete
  4. I have implemented multitenant with shards. Everything is fine exception transaction management. The transaction is not rolled back in multitenant shards. However, it is rolled back in case of without murltitenat shards. Does using multitenant shards changes properties of Transaction manager? How can this be solved?



    #Spring configuration files to be loadded. By adding shard-data-source-spring.xml in the list database sharding feature
    #can be enabled
    spring.configs=\
    META-INF/base-spring.xml,\
    \
    META-INF/hibernate-spring.xml,\
    META-INF/infrastructure-spring.xml,\
    META-INF/management-spring.xml,\
    \
    META-INF/util-spring.xml,\
    \
    META-INF/jpa-spring.xml,\
    \
    META-INF/executor-spring.xml,\
    \
    META-INF/audit-spring.xml,\
    META-INF/cluster-spring.xml,\
    META-INF/editor-spring.xml,\
    META-INF/jcr-spring.xml,\
    META-INF/ldap-spring.xml,\
    META-INF/messaging-core-spring.xml,\
    META-INF/messaging-misc-spring.xml,\
    META-INF/mobile-device-spring.xml,\
    META-INF/notifications-spring.xml,\
    META-INF/poller-spring.xml,\
    META-INF/rules-spring.xml,\
    META-INF/scheduler-spring.xml,\
    META-INF/scripting-spring.xml,\
    META-INF/search-spring.xml,\
    META-INF/workflow-spring.xml,\
    \
    META-INF/counter-spring.xml,\
    META-INF/mail-spring.xml,\
    META-INF/portal-spring.xml,\
    META-INF/portlet-container-spring.xml,\
    META-INF/staging-spring.xml,\
    META-INF/virtual-layouts-spring.xml,\
    META-INF/shard-data-source-spring.xml

    shard.selector=com.liferay.portal.dao.shard.ManualShardSelector
    shard.available.names=default,one,two

    ReplyDelete
  5. Amit, I followed the post but when I perform the deploy I get the error ClassNotFoundException for hibernate classes. So I tried putting the jar but I get the error of ClassCastException. Just using eclipse wtp and I could test the portlet.

    ReplyDelete
  6. So, i want to connect to two database, it's useful for me, please explain to me

    ReplyDelete
  7. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    best java training in velachery |
    java training in velachery

    ReplyDelete

nRelate Posts Only