The batch requests feature allows you to send multiple (IN-Only) requests to a datasource using a single operation (batch operation). Follow the steps given below to define a data service that can invoke batch requests:
Setting up a datasource
Follow the steps given below to set up a MySQL database for this tutorial.
- Download the product installer from here, and run the installer.Error rendering macro 'excerpt-include' : User 'null' does not have permission to view the page 'EI6xx:Installing the Product'.
- Install the MySQL server.
- Download the JDBC driver for MySQL from here and copy it to your
<EI_HOME>/lib
directory. Create the following database: Company
CREATE DATABASE Company;
Create the Employees table:
USE Company; CREATE TABLE `Employees` (`EmployeeNumber` int(11) NOT NULL, `FirstName` varchar(255) NOT NULL, `LastName` varchar(255) DEFAULT NULL, `Email` varchar(255) DEFAULT NULL, `JobTitle` varchar(255) DEFAULT NULL, `OfficeCode` int(11) NOT NULL, PRIMARY KEY (`EmployeeNumber`,`OfficeCode`));
Define a data service to insert records in batches
Let's create a data service using the Create Data Service wizard:
Start the WSO2 ESB profile.
- Open the ESB profile's Management Console using
https://localhost:9443/carbon
, and log in usingadmin
as the username and the password. - Click Data Service > Create.
- Add a name for the data service, such as
BatchRequestDataService
. - Select the Enable Batch Requests check box.
- Click Next and add a new datasource.
Give an ID for the datasource, and create the connection to the Company database.
Datasource ID Datasource Datasource Type RDBMS Datasource Type (Default/External) Leave Default selected. Database Engine MySQL Driver Class com.mysql.jdbc.Driver
URL jdbc:mysql://localhost:3306/Company
User Name Enter your MySQL server's username. Password Enter your MySQL server's password.
If you have not assigned a password, keep this field empty.Example:
- Save the datasource details, and click Next to open the Queries screen.
Click Add New Query to specify the query details:
Enter addEmployeeQuery as the query ID.
Enter the following SQL dialect:
insert into Employees (EmployeeNumber, FirstName, LastName, Email, JobTitle, OfficeCode) values(:EmployeeNumber,:FirstName,:LastName,:Email,:JobTitle,:Officecode)
Click Generate Input Mapping and input mappings will be generated automatically for the EmployeeNumber, FirstName, LastName, Email, JobTitle, and OfficeCode fields.
Save the query, and click Next to open the Operations screen.
- Click Add New Operation, and create an operation for the addEmployeeQuery query as shown below.
- Save the operation.
- Click Finish to complete the data service creation process.
Invoking the 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 Try this Service to open the data service from the TryIt tool.
- Click Try this Service to open the data service from the TryIt tool. There will be an addemployeeOp_batch_request operation automatically generated.
- Select the batch operation.
Enter multiple transactions as shown below. In this example, we are sending two transactions with details of two employees.
<p:addEmployeeOp_batch_req xmlns:p="http://ws.wso2.org/dataservice"> <!--1 or more occurrences--> <addEmployeeOp xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1002</xs:EmployeeNumber> <!--Exactly 1 occurrence--> <xs:FirstName xmlns:xs="http://ws.wso2.org/dataservice">John</xs:FirstName> <!--Exactly 1 occurrence--> <xs:LastName xmlns:xs="http://ws.wso2.org/dataservice">Doe</xs:LastName> <!--Exactly 1 occurrence--> <xs:Email xmlns:xs="http://ws.wso2.org/dataservice">johnd@wso2.com</xs:Email> <!--Exactly 1 occurrence--> <xs:JobTitle xmlns:xs="http://ws.wso2.org/dataservice">Consultant</xs:JobTitle> <!--Exactly 1 occurrence--> <xs:Officecode xmlns:xs="http://ws.wso2.org/dataservice">01</xs:Officecode> </addEmployeeOp> <addEmployeeOp xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1004</xs:EmployeeNumber> <!--Exactly 1 occurrence--> <xs:FirstName xmlns:xs="http://ws.wso2.org/dataservice">Peter</xs:FirstName> <!--Exactly 1 occurrence--> <xs:LastName xmlns:xs="http://ws.wso2.org/dataservice">Parker</xs:LastName> <!--Exactly 1 occurrence--> <xs:Email xmlns:xs="http://ws.wso2.org/dataservice">peterp@wso2.com</xs:Email> <!--Exactly 1 occurrence--> <xs:JobTitle xmlns:xs="http://ws.wso2.org/dataservice">Consultant</xs:JobTitle> <!--Exactly 1 occurrence--> <xs:Officecode xmlns:xs="http://ws.wso2.org/dataservice">01</xs:Officecode> </addEmployeeOp> </p:addEmployeeOp_batch_req>
Execute the batch operation. You will find that all the records have been inserted into the database simultaneously.
Want to confirm that the records are added to the database? Run the following MySQL command.
SELECT * FROM Employees
You see all the records that are in the
Employees
table, including the two employee records that you added in the previous step.