Skip to main content

Writing a Data Service to execute a stored procedure with WSO2 DSS

The WSO2 Data Services Server augments Service Oriented Architecture (SOA) development efforts by providing an easy to use platform for integrating data stores, creating composite data views, and hosting data services. This blog post is written to help users to getting started with WSO2 DSS with writing a data service to execute a stored procedure which is defined in a database.


Prerequisites

Before starting the implementation, you need the following things to be downloaded and installed.
1. Download WSO2 Data Services Server and install.
2. Download mysql server and install.
Once you have installed both WSO2 DSS and mysql in your enviornment, you can proceed to the next step.

Creating the stored procedure in mysql

1. Connect to mysql server with the following command.
mysql -u root -p
Enter password:

2. Create a sample databasae.
DROP DATABASE IF EXISTS ESB_SP_SAMPLE;
CREATE DATABASE ESB_SP_SAMPLE;

3. Create a table using the following statement.
USE ESB_SP_SAMPLE;

DROP TABLE IF EXISTS company;
CREATE TABLE company(name VARCHAR(10), id VARCHAR(10), price DOUBLE, location VARCHAR(10));

4. Inserts some data to the company table using following statements
INSERT INTO company VALUES ('WSO2','c1',2.9563,'SL');
INSERT INTO company VALUES ('IBM','c2',3.7563,'US');
INSERT INTO company VALUES ('SUN','c3',3.8349,'US');
INSERT INTO company VALUES ('MSFT','c4',3.2938,'US');

5. Create necessary Stored Procedures.

DROP PROCEDURE If EXISTS InsertRequestData;
CREATE PROCEDURE InsertRequestData(compName VARCHAR(10), compId VARCHAR(10), compPrice DOUBLE, compLocation VARCHAR(10)) INSERT INTO company VALUES(compName,compId,compPrice,compLocation) ;

Configuring the DSS to work with the mysql database

Download the mysql connector library from here and copy that file in to DSS_HOME/lib/extensions/ (create a directory if not exists) directory.

If you are a Windows user, you need to run wso2server.bat file, or, for a Linux user you need to run wso2server.sh script using command prompt or shell to start WSO2 Data Services server.

Writing the Data service to access the stored procedure

Now you have configured the DSS to work with mysql and created the stored procedure. Then you need to create the dataservice configuration file as below.

StockQuoteDataService.dbs
----------------------------------------
<data enableBatchRequests="true" name="StockQuoteDataService">
  <description>Sample Data Service</description>
  <config id="DemoDataSource">
     <property name="driverClassName">com.mysql.jdbc.Driver</property>
     <property name="url">jdbc:mysql://localhost:3306/ESB_SP_SAMPLE</property>
     <property name="username">root</property>
     <property name="password">root123</property>
  </config>
  <query id="insertRequestData" useConfig="DemoDataSource">
     <sql>Call ESB_SP_SAMPLE.InsertRequestData(?,?,?,?)</sql>
     <properties>
        <property name="org.wso2.ws.dataservice.query_timeout">100</property>
        <property name="org.wso2.ws.dataservice.force_jdbc_batch_requests">true</property>
     </properties>
     <param name="name" ordinal="1" sqlType="STRING"/>
     <param name="id" ordinal="2" sqlType="STRING"/>
     <param name="price" ordinal="3" sqlType="DOUBLE"/>
     <param name="location" ordinal="4" sqlType="STRING"/>
  </query>   
  <operation name="insertRequestData" returnRequestStatus="false">
     <call-query href="insertRequestData">
   <with-param name="name" query-param="name"/>
        <with-param name="id" query-param="id"/>
        <with-param name="price" query-param="price"/>
        <with-param name="location" query-param="location"/>        
     </call-query>
  </operation>  
</data>

In this configuration file, you can find 3 sections which are used to create the data service.

<config id="DemoDataSource">
     <property name="driverClassName">com.mysql.jdbc.Driver</property>
     <property name="url">jdbc:mysql://localhost:3306/ESB_SP_SAMPLE</property>
     <property name="username">root</property>
     <property name="password">root123</property>
  </config>

This is the data source configuration section which defines the database related information.

  <query id="insertRequestData" useConfig="DemoDataSource">
     <sql>Call ESB_SP_SAMPLE.InsertRequestData(?,?,?,?)</sql>
     <properties>
        <property name="org.wso2.ws.dataservice.query_timeout">100</property>
        <property name="org.wso2.ws.dataservice.force_jdbc_batch_requests">true</property>
     </properties>
     <param name="name" ordinal="1" sqlType="STRING"/>
     <param name="id" ordinal="2" sqlType="STRING"/>
     <param name="price" ordinal="3" sqlType="DOUBLE"/>
     <param name="location" ordinal="4" sqlType="STRING"/>
  </query>   

This is the actual query which is going to execute in the database once this data service is called.

  <operation name="insertRequestData" returnRequestStatus="false">
     <call-query href="insertRequestData">
   <with-param name="name" query-param="name"/>
        <with-param name="id" query-param="id"/>
        <with-param name="price" query-param="price"/>
        <with-param name="location" query-param="location"/>        
     </call-query>
  </operation>  

This part defined the actual operation clients are calling when they are accessing the data service from client side. This includes all the input parameters and output parameters for the query.

Now save this configuration in a file called StockQuoteDataService.dbs and save that file in DSS_HOME/repository/deployment/server/dataservices directory. Now you can see in the DSS console, the data service is deployed successfully. 

Accessing the data service

You can use the tryIt tool to access the data service and see your data is updating to the database. 

Cheers !!!!

Comments

  1. hi, how can I write the client code with the wsdl file of the service?? can you provide some sample codes?

    ReplyDelete
  2. Hi, Can we insert parent and child data (nested data) using store procedure

    ReplyDelete
  3. Hi Have you tried making that into a rest resource? Especially with a stored procedure that has an out param? I am getting errors because i don't know how to eliminate the out param from the query parameters in the resource section.

    ReplyDelete
  4. Interesting walkthrough because executing stored procedures through data services feels simple until performance and maintainability start becoming part of the conversation. One thing I kept wondering is whether modern architectures are moving away from direct database logic or just exposing it differently. It feels like enterprise data services are increasingly focused on making data access more reusable and connected across systems. Curious how you think that approach has evolved over time.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to protect your APIs with self contained access token (JWT) using WSO2 API Manager and WSO2 Identity Server

In a typical enterprise information system, there is a high chance that people will use different types of systems built by different vendors to implement certain types of functionalities. The APIs might be hosted in an API Manager developed by vendor A and the user management can be implemented using a different vendor (vendor B). In this type of a situation, one system will not be able to directly contact the other system but they want to use both systems in tandem. Self-contained access tokens are used in these types of situations where applications can get the token from one system and use that in another system to access protected resources. In this scenario, the second system does not need to make a contact to the first system over the network to validate the user information since the token is self-contained and it has relevant details about the user. This will improve the token processing time significantly since it completely removes the network interaction. The below fig...

WSO2 ESB creating a response for a "GET" request

When you are creating REST APIs with WSO2 ESB, you may need to send some response message back to the user when something goes wrong. In this kind of scenario, you can create a payload inside the ESB and send it back. For doing this, you can use the below configuration. <api xmlns=" http://ws.apache.org/ ns/synapse " name="LoopBackProxy" context="/loopback">    <resource methods="POST GET">       <inSequence>          <property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>          <log level="full"></log>          <payloadFactory media-type="xml">             <format>                <m:messageBeforeLoopBack xmlns:m=" http://services...

WSO2 ESB usage of get-property function

What are Properties? WSO2 has a huge set of mediators but property mediator is mostly used mediator for writing any proxy service or API. Property mediator is used to store any value or xml fragment temporarily during life cycle of a thread for any service or API. We can compare “Property” mediator with “Variable” in any other traditional programming languages (Like: C, C++, Java, .Net etc). There are few properties those are used/maintained by ESB itself and on the other hand few properties can be defined by users (programmers). In other words, we can say that properties can be define in below 2 categories: ESB Defined Properties User Defined Properties. These properties can be stored/defined in different scopes, like: Transport Synapse or Default Axis2 Registry System Operation Generally, these properties are read by get-properties() function. This function can be invoked with below 2 variations. get-property(String propertyName) get-property(String scop...