Creating DataSources on WSO2 Enterprise Integrator

When dealing with databases the most basical requirement is to be able to connect to it. In the WSO2 products we use the Data Sources to create connection pools to be used by the components that require database connectivity like DataServices.

In this post we will show some ways to create datasources on WSO2 Enterprise Integrator. The examples used the WSO2 Enterprise Integrator 6.1.1 but they may be applied to the other products as they share the same core components.

Let’s start.

1. Using the Carbon Console

The first one that we will see is using the carbon console.

In the Configure Tab we see an option called “Datasources” where we can see all the created datasources and also give us the ability to add new ones.

We can find the explanation about this way in the WSO2 Docs Here.

2. Using the Admin Services

WSO2 EI comes with some services that allow us to perform some administrative operations, one of them is deal with datasources.

To see all admin services available in the installation, we need to start the product with the environment variable -DosgiConsole, like below:

[WSO2_HOME]/bin/integrator.bat -DosgiConsole

By passing this variable it will start the osgi console, on it we can use the command listAdminServices that is going to list all admin services available, the output will be like below:

osgi> listAdminServices
Admin services deployed on this server:
1. ModuleAdminService, ModuleAdminService, https://localhost:8243/services/ModuleAdminService
2. InboundAdmin, InboundAdmin, https://localhost:8243/services/InboundAdmin
3. TemplateAdminService, TemplateAdminService, https://localhost:8243/services/TemplateAdminService
4. CustomUIAdminService, CustomUIAdminService, https://localhost:8243/services/CustomUIAdminService
5. ThemeMgtService, ThemeMgtService, https://localhost:8243/services/ThemeMgtService
6. MediationLibraryUploader, MediationLibraryUploader, https://localhost:8243/services/MediationLibraryUploader
7. LoginStatisticsAdmin, LoginStatisticsAdmin, https://localhost:8243/services/LoginStatisticsAdmin
8. PropertiesAdminService, PropertiesAdminService, https://localhost:8243/services/PropertiesAdminService
9. PublishEventMediatorConfigAdmin, PublishEventMediatorConfigAdmin, https://localhost:8243/services/PublishEventMediatorConfigAdmin
10. LoggingAdmin, LoggingAdmin, https://localhost:8243/services/LoggingAdmin
11. LogViewer, LogViewer, https://localhost:8243/services/LogViewer
12. EventStreamAdminService, EventStreamAdminService, https://localhost:8243/services/EventStreamAdminService
13. NDataSourceAdmin, NDataSourceAdmin, https://localhost:8243/services/NDataSourceAdmin
14. DataServiceAdmin, DataServiceAdmin, https://localhost:8243/services/DataServiceAdmin
15. SearchAdminService, SearchAdminService, https://localhost:8243/services/SearchAdminService
16. RestApiAdmin, RestApiAdmin, https://localhost:8243/services/RestApiAdmin
17. WSDL2CodeService, WSDL2CodeService, https://localhost:8243/services/WSDL2CodeService
18. CustomMeteringService, CustomMeteringService, https://localhost:8243/services/CustomMeteringService

The one that we are looking for is NDataSourceAdmin. This service allow us to perform some operation on the Datasources in the installation.

We can import this SOAP Service to SOAP UI to try it out. We just need to use the service url provided in the osgi console and append ?wsdl.

TIP: In order to be able to see the WSDL of the admin services we need to change the flag below in carbon.xml to false:

<HideAdminServiceWSDLs>false</HideAdminServiceWSDLs>

By importing the wsdl to the SOAP UI we can see all the operations available, the one that we are looking for is addDataSource.

To create the datasource we are going to have a SOAP Payload like below:

Basically we are defining the datasource name, jndi name and all required properties to connect to the database like driver, jdbc url, username, password, and so on. Please note that the values enclosed by “${}” need to be changed to your own settings, you can also change the other properties as well.

As it is an Admin service in order to invoke it we need to be authenticated. The simpler way is to add a Basic Authentication header containing the admin username and password.

In SOAP UI we can add it in the Authorization settings like in the image below:

SOAP UI Auth

One big benefit of those services is that we can automate the Datasource creation using some scripts to invoke the services.

3. Creating a *-datasources.xml file

The third way we are going to present in this post is by creating a file named like “*-datasources.xml”. This file will contain the datasource definition. Basically we can take the datasource definition files we have in the installation in the folder conf/datasources.

We just need to place the file with that naming convetion and put the database configs to create the datasource, we can see an example below, we can name it, for example, as my-datasources.xml:

After placing the file in conf/datasources and restart the WSO2 instance, we will see the datasource created in the carbon console.

Carbon Console

Just discovered that was possible when I found the repository for the Carbon Datasources implementation Here.

That’s it for today, I hope you enjoyed it.

See you in the next post.

comments powered by Disqus