Saturday, April 25, 2015

Connecting to different Data Sources


Connecting to different Data Sources

Often we use to come across a situation as dozen of application running in any enterprises and each of them meeting some specific need of the organization. Now suddenly we may need to create a portal in which we should not disturb any of the existing legacy applications and databases they connect to. At the same time, our new portal has to fetch information from these different databases originally used by the legacy system.

connecting to different Data Source can be achieved in following steps :

Step 1: put the below code either in portal “portal-setup-wizard.properties” or “portal-ext.properties” under PORTAL_HOME and restart the server.



the above entries is use for another data base.
NOTE : you can connect to any kind of data base like sql server, mysql etc. All you have to do is provide the data base configuration in the above mentioned file.

Step 2 : In your portlet create a new file “ext-spring.xml” inside “WEB- INF/src/META-INF” and paste the below contents.



Step 3 : Define a new entity in Service.xml which will mapped to another data base but not the default one.


In the above entity there is some new attribute data-source and session-factory are mandatory. These are the attributes which will look for ext-spring.xml file and fetch the configuration for connecting another data base. Save file and re-generate service layer.

Step 4: Unlike the entities that are mapped to the default data-source, the table for this
entity will not be created automatically. We need to create it manually as i have use mysql data base so accordingly i am writing query here.

CREATE TABLE `anotherdatabase`.`temporaryemployee` (
  `employeeId` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `employeeName` VARCHAR(45) NOT NULL DEFAULT '',
  `employeeAddress` VARCHAR(45) NOT NULL DEFAULT '',
  PRIMARY KEY(`employeeId`)
)
ENGINE = InnoDB;


Step 5 : Now create an action class as below : 


Step 6 : And jsp should be as follows:



now after building services and deploying portlet try to save a record.
check in your newly create table that record has inserted successfully.

In the same way you can fetch record from already created Table  all you have to do is create entity and give attribute table with same name for the table of which data you want to fetch.This way you can write application code to fetch data from another Data Base table.


0 comments:

Post a Comment