Generating a Data Service
This option automatically creates a data service using a given database structure. When generating a data service, the server takes its table structure according to the structure specified in the datasource and automatically creates the SELECT
, INSERT
, UPDATE
, and DELETE
operations.
Step 1: 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.
Let's call the installation location of your product the <EI_HOME> directory.If you installed the product using the installer, this is located in a place specific to your OS as shown below:
OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.5.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.5.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.5.0
CentOS /usr/lib64/EnterpriseIntegrator/6.5.0
- 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:
AccountDetails
CREATE DATABASE AccountDetails;
Create the following table:
USE AccountDetails; CREATE TABLE ACCOUNT(AccountID int NOT NULL,Branch varchar(255) NOT NULL, AccountNumber varchar(255),AccountType ENUM('CURRENT', 'SAVINGS') NOT NULL, Balance FLOAT,ModifiedDate DATE,PRIMARY KEY (AccountID));
Enter the following data into the table:
INSERT INTO ACCOUNT VALUES (1,"AOB","A00012","CURRENT",231221,'2014-12-02');
Step 2: Creating a Carbon datasource
The ESB profile of WSO2 EI comes with the datasource management feature, which allows users to create any RDBMS datasource or custom datasource that is used by the server to connect to databases or external data stores. See Managing Datasources in the WSO2 Administration Guide for instructions on how to use this feature. Note that these pre-defined datasources can also be added to a data service as Carbon datasources.
Follow the steps given below.
Start the ESB profile.
- Access the management console:
https://localhost:9443/cabon
- Sign in using
admin
as the username and password. - Open the Configure tab and click Datasources.
This will open the Datasources screen. Click Add Datasource and enter the following details corresponding to the
AccountDetails
database:Datasource Type RDBMS Name AccountDetails Datasource provider Leave Default selected. Database Engine MySQL Driver com.mysql.jdbc.Driver
URL jdbc:mysql://localhost:3306/AccountDetails
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.Want to make sure that your datasource is connected to the database? Click Test Connection to find out.
- Save the datasource.
Step 3: Generating a data service
Once the datasource is created, follow the steps below to generate a data service.
- Open the Main tab and click Data Service > Generate.
- In the Carbon Datasource field, select the
AccountDetails
datasource from the drop-down list. In the Database Name field, give the name of the database that you created, which is
AccountDetails
.Note that in most cases the database name should be the same name used when creating the datasource. If you want to find the database name for the datasource you selected in step 2 above, go to the Configure tab and click Datasources in the navigator. You can then get the details (including the database name) of a datasource by clicking View.
However, some datasources such as Oracle and H2 may not require the exact database name.- Click Next to open the Customize Service Generation screen.
The tables in the AccountDetails database will be listed. We only have one table (ACCOUNT) in the database as shown below.
- Select the ACCOUNT table and click Next to open the Service Generation screen.
- You must select a service generation mode from the following two options:
For this guide select Multiple Services. A data service is created for the table in the AccountDetails datasource.- Single Service: Creates a single data service for operations of all tables.
If this option is selected, you need to provide a name for the Data Service you are creating. - Multiple Services: Creates a service per table, which will contain isolated operations for each table.
This is only applicable if you have selected multiple tables in the previous step.
- Single Service: Creates a single data service for operations of all tables.
- Click Next to open the Generated Services screen. A data service by the name of ACCOUNT_DataService is now generated.
- 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 4: Verifying the generated CRUD operations (Optional)
You can verify that the relevant queries and CRUD operations are generated accurately:
- Click the ACCOUNT_DataService service to open the service dashboard.
- Click Edit Data Service (Wizard) to open the service in the Create New Data Service wizard.
- Click Next until you get to the Queries screen. The queries corresponding to CRUD operations are automatically generated:
- Click Next to get to the Operations screen. The CRUD operations connected to the queries:
- Click Finish to go back to the service dashboard.
Step 5: 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 AccountDetails data service. The TryIt Tool will open with the data service.
See that the CRUD operations are generated for the datasource.Execute the CRUD operations from the TryIt tool to see the result.