The data integration feature in the ESB profile of WSO2 EI supports data federation, which means that a single data service can expose data from multiple datasources. However, if you have multiple RDBMSs connected to your data service, and if you need to perform IN-ONLY operations (operations that can insert data and modify data in the datasource) in a coordinated manner, the RDBMSs need to be defined as XA datasources. Consider a scenario where you have two MySQL databases. You can define a single data service for these databases and insert data into both as explained below.
...
- Install the MySQL server.
- Download the JDBC driver for MySQL from here and copy it to your
<EI_HOME>/lib
directory. - Set up a database for storing information of offices:
Create a database called OfficeDetails.called OfficeDetails:
CREATE DATABASE OfficeDetails;
Create the the Offices table table:
Code Block USE OfficeDetails; CREATE TABLE `OFFICES``Offices` (`OfficeCode` int(11) NOT NULL, `AddressLine1` varchar(255) NOT NULL, `AddressLine2` varchar(255) DEFAULT NULL, `City` varchar(255) DEFAULT NULL, `State` varchar(255) DEFAULT NULL, `Country` varchar(255) DEFAULT NULL, `Phone` varchar(255) DEFAULT NULL, PRIMARY KEY (`OfficeCode`));
- Set up a database for storing information of employees:
- Create a database called EmployeeDetails.called EmployeeDetails:
CREATE DATABASE EmployeeDetails;
Create the the Employees table table:
Code Block USE EmployeeDetails; CREATE TABLE `EMPLOYEES``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`));
- Create a database called EmployeeDetails.called EmployeeDetails:
...
- Log into the management console of WSO2 EI and the ESB profile and click Create under Data Service.
- Add a name for the data service and go to the next step.
Select the Enable Boxcarring and Disable
Legacy Boxcarring Mode check box.Click Next to go to the Datasources screenLegacy Boxcarring Mode check boxes.
Note The Enable boxcarring option enables both the legacy boxcarring and the new request box methods. You can disable the legacy boxcarring method and use the request box method by selecting the Disable Legacy Boxcarring Mode option.
- Click Next to go to the Datasources screen.
- Create a datasource connection for the the CompanyDetails database database follows:
- Enter Enter XAoffices as as the datasource ID.
- Select Select RDBMS as as the datasource type.
- Select Select External Datasource from from the adjoining field.
- Select Select MySQL as as the database engine.
- Enter the following database class corresponding to MySQL:
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
Click Click Add Properties
and createand specify the following connection settings for
the OfficeDetails database.
URL: jdbc:the OfficeDetails database:
USER: rootPropertyName Value url jdbc:mysql://localhost:3306/OfficeDetails
user root
- Save the XAoffices datasource.
- Create a datasource connection for the the EmployeeDetails database as follows:
- Enter XAemployees as the datasource ID.
- Select RDBMS as the datasource type.
- Select External Datasource from the adjoining field.
- Select MySQL as the database engine.
- Enter the following database class corresponding to MySQL:
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
Click Add Properties and
createspecify the following connection settings for the
OfficeDetails database.
URL: jdbc:EmployeeDetails database:
USER: rootProperty Name Value url jdbc:mysql://localhost:3306/EmployeeDetails
user root
- Save the XAemployees datasource.
...
Defining queries for the datasources
- Click Add New Query to specify an insert query for the the XAoffices datasource:
Enter InsertOfficeQuery as the query ID.
Enter the following SQL dialect:
Code Block insert into Offices (OfficeCode,AddressLine1,AddressLine2,City,State,Country,Phone) values(:OfficeCode,:AddressLine1,'test','test','test','USA','test')
- Click Generate Input Mapping and an input mapping will be generated automatically for the fields in the datsource:
- Save the query.
- Click Add New Query to specify an insert query for the the XAemployees datasource:
Enter Enter InsertEmployeeQuery as the query ID.
Enter the following SQL dialect:
Code Block insert into Employees (employeeNumber,firstName,lastName,email,jobtitle,OfficeCode) values(:employeeNumber,:firstName,:lastName,'test','test',:OfficeCode)
- Click Generate Input Mapping and an input mapping will be generated automatically for the fields in the datasource:
- Save the query.
- Click Click Next to to go to the the Operations section section. Define operations to invoke the two queries defined above.
- Create the the InsertOfficeOp for the for the InsertOfficeQuery.
- Create the the InsertEmployeeOp for the for the InsertEmployeeQuery.
- Finish creating the data service.
...
Inserting data into the distributed RDBMSs
- Go to the the Deployed Services page page and you will see the data service listed.
- Click Click Try this service to to open the TryIt tool.
- Select the batch operation that is created by default (request_box_operation).
- Specify the values that should be inserted to the the OfficeDetails database and database and EmployeeDetails database database respectively.
- Invoke the operation.
- See that the data is successfully inserted into the two databases.
- Now, enter another set of values for the two operations. However, you can enter an erroneous value for one fieldmake one operation erroneous. For example, leave the OfficeCode field of the InsertEmployeesOp blank.
- Invoke the request box operation.
- See that no records have been entered into either database.