In this tutorial, we will run through the process of service enabling an RDBMS as a data service.
Step 1: Setting up an RDBMS
Follow the steps given below to set up a MySQL database for this tutorial.
- Install the MySQL server.
- Download the JDBC driver for MySQL from here and copy it to your
- Create the following database:
Create the following table:
Enter the following data into the table:
Step 2: Adding an RDBMS datasource
Follow the steps given below.
In the Datasource Type field, select RDBMS and Default from the lists.
If you enter External instead of the Default datasource type, your datasource should be supported by an external provider class, such as
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource.You can select the External option and enter the name and value of connection properties by clicking Add Property. For example,
After an external datasource is created, it can be used as a usual datasource in queries. See the tutorial on handling distributed transactions for more information on using external datasources.
- Enter MySQL in the Database Engine field.
Enter the following driver class for MySQL in the Driver Class URL field: com.mysql.jdbc.Driver.
Be sure to download the JDBC driver for MySQL from here and copy it to your
If the driver class does not exist in the relevant folders when you create the datasource, you will get an exception such as 'Cannot load JDBC driver class com.mysql.jdbc.Driver'.
See the section on working with RDBMS datasources, for more options that you can use with your RDBMS data service.
Enter the URL to your MySQL database in the URL field: jdbc:mysql://localhost:3306/trainingdb
Enter the username and password to connect to your MySQL database. By default the username is root and the password is blank.
Step 3: Defining a query
Now let's start writing a query for getting data from the datasource. The query will specify the data that should be fetched and the format that should be used to display data when the query is invoked.
- Click Add New Query to open the Add New Query screen.
- Enter the following values:
- Query ID: Enter an ID for the query.
- Datasource: Select the datasource for which you are going to write a query. Select the RDBMS datasource that you created previously.
SQL: In this field, enter the SQL statement describing the data that should be retrieved from the RDBMS datasource.
Add input mappings: Click Generate Input Mapping to automatically create a mapping for the AccountID field in the database.
Input mappings allow you to add parameters to a query so that you can set the parameter value when executing the query. According to the above definition, you need to provide the account ID as an input in order to retrieve the data corresponding to the account ID.
Find out more about defining Input Mappings in WSO2 DSS.
Add output mappings: Click Generate Response to automatically creating mappings for the fields that should show in the output.
Output mapping is used to specify how the data that is fetched from your query will be shown in the response. Note that, by default, the output type is XML. Find out more about defining Output Mappings in WSO2 DSS.
Step 4: Defining an operation to invoke the query
Data service operations are written to invoke queries.
- Click Add New Operation to open Add New Operation screen.
- Add a name for your operation in the Operation Name field.
- In the Query ID field, select the query that you defined previously.
- Save the operation.
Step 5: Finish creating the data service
Once you have defined the operation, click Finish to complete the data service creation process. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.
Step 6: Invoking your data service
You can try the data service you created by using the TryIt tool that is in your product by default.
- Go to the Deployed Services screen.
- Click the Try this service link for the AccountDetailsService. The TryIt Tool will open with the data service.
- Select the operation you created earlier. You need to provide the account ID as an input. Enter '1'.
Click Send to see the details of the account.