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.
- 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 the Employee table:
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`), CONSTRAINT `employees_ibfk_1` FOREIGN KEY (`OfficeCode`) REFERENCES `OFFICES` (`OfficeCode`));
Define a data service to insert records in batches
Let's create a data service using the Create Data Service wizard:
- Log into the management console of WSO2 EI and click Create in the Data Service menu.
- Add a name for the data service and select the Enable Batch Requests check box as shown below.
- Click Next and add a new datasource.
- Connect to the Company database that you defined above.
- 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, lastName, firstName, email, salary) values(:employeeNumber,'test','test',:email,:officecode)
Click Generate Input Mapping and an input mapping will be generated automatically for the employeeNumber, email, and officecode field.
Create an operation for the addEmployeeQuery query as shown below.
Click Finish to navigate to the Deployed Services window, from where you can manage data services.
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 a batch_request operation automatically generated.
- Select the batch operation.
Enter multiple transactions as shown below. In this example, we are sending two transactions.
<p:addEmployeeQueryOP_batch_req xmlns:p="http://ws.wso2.org/dataservice"> <!--1 or more occurrences--> <addEmployeeQueryOP xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:employeeNumber xmlns:xs="http://ws.wso2.org/dataservice">100</xs:employeeNumber> <!--Exactly 1 occurrence--> <xs:email xmlns:xs="http://ws.wso2.org/dataservice">abc@email.com</xs:email> <!--Exactly 1 occurrence--> <xs:officecode xmlns:xs="http://ws.wso2.org/dataservice">1</xs:officecode> </addEmployeeQueryOP> <addEmployeeQueryOP xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:employeeNumber xmlns:xs="http://ws.wso2.org/dataservice">101</xs:employeeNumber> <!--Exactly 1 occurrence--> <xs:email xmlns:xs="http://ws.wso2.org/dataservice">abd@email.com</xs:email> <!--Exactly 1 occurrence--> <xs:officecode xmlns:xs="http://ws.wso2.org/dataservice">2</xs:officecode> </addEmployeeQueryOP> </p:addEmployeeQueryOP_batch_req>
- Execute the batch operation. You will find that all the records have been inserted into the database simultaneously.