Thursday, November 11, 2010

Creating a simple data service using WSO2 Data Services Server

Here I am going to illustrate, how to use WSO2 Data Services Server for exposing data in a mysql database as a service.


  • WSO2 Data Services Server - download it from here 

  • MySQL 5 or later version - download it from here

  • JDK 1.5 or Higher

Let's create a database with a single table. Below I have given a mysql script for creating database with some data.

-- Create schema AccountDB

USE AccountDB;

-- Definition of table `AccountDB`.`account`

CREATE TABLE  `AccountDB`.`account` (
  `account_no` int(10) unsigned NOT NULL,
  `name` varchar(45) NOT NULL,
  `balance` int(11) NOT NULL

-- Dumping data for table `AccountDB`.`account`

INSERT INTO `AccountDB`.`account` VALUES  (1,'Michael',2000),

Here I have given the data services configuration file that I used.  You can refer this  documentation for creating the data service using data services server user interface, or you can write the dbs configuration file and upload it to data services server. Since we are using a MySQL database, you should copy mySQL JDBC driver to DSS_HOME/repository/components/lib

Here I have given my data services configuration file

<data name="AccountDataService" enableBatchRequests="false" enableBoxcarring="false" serviceStatus="active">  
   <config id="Account">     
      <property name="">

      <property name="">

      <!-- use your own mysql database username -->      
      <property name="">root</property>    

      <!-- use your own mysql database password -->     
      <property name="">password</property>      </config>  
   <query id="AccountDetails" useConfig="Account">     
      <sql>select account_no, name, balance from account </sql>     
      <result element="account_details" rowName="account"> 

         <!-- mapping database columns to output field-->       
         <element name="accountNo" column="account_no" xsdType="xs:integer" />        
         <element name="name" column="name" xsdType="xs:string" />        
         <element name="balance" column="balance" xsdType="xs:integer" />     
   <operation name="getAccountDetails">     
      <call-query href="AccountDetails" />  

After uploading the dbs file to the Data Services Server, Now you can see the Newly added AccountDataService in the services page. (Go to Manage->Services->List). Then you can try the service using "Try it".



Anonymous said...

That is the easiest case. What will happens ifthe data returned by the SQL query have some columns set to null. I gat a Null pointerException. Would be interested if you can explore that case as well.

Anonymous said...

Hi Kasun's,

I have one Query
Here i am sending the my GIT URL which contain my Query

Could you please help me.
Thanks in Advance