[WSO2] How to Add a Datasource by Commandline

Every server at WSO2 (i.e Application Server, Data services Server) has a common way of exposing a database to its applications. That is using a data source. Sometimes, we do not prefer to create datasource in server by logging into the Management Console. Programmatically, we should be able to create a data source.

Admin Services exposed by the server 



There are various admin services exposed by the server as SOAP. We can leverage one of such admin service called “NDataSourceAdmin” to manage data-sources programmatically.
This admin service has following operations.



Calling Admin Services



We can call the SOAP admin services by sending a relevant soap request with the action header set to the operation name we are calling. For an example if we want to add a datasource to the server,
SOAP action: addDataSource


SOAP request to be sent when adding a datasource


Following is a sample data-source wrapped in a SOAP envelope.


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://org.apache.axis2/xsd" xmlns:xsd1="http://services.core.ndatasource.carbon.wso2.org/xsd" xmlns:xsd2="http://core.ndatasource.carbon.wso2.org/xsd">
   <soapenv:Header/>
   <soapenv:Body>
<xsd:addDataSource xmlns:xsd="http://org.apache.axis2/xsd"
    xmlns:xsd1="http://services.core.ndatasource.carbon.wso2.org/xsd"
    xmlns:xsd2="http://core.ndatasource.carbon.wso2.org/xsd">
    <xsd:dsmInfo>
        <xsd1:definition>
            <xsd1:dsXMLConfiguration>
                   <![CDATA[<configuration>
                  <url>jdbc:mysql://localhost:3306/dbname</url>
                  <username>root</username>
                  <password>root</password>
                  <driverClassName>com.mysql.jdbc.Driver</driverClassName>
                  <maxActive>50</maxActive>
                  <maxWait>60000</maxWait>
                  <testOnBorrow>true</testOnBorrow>
                  <validationQuery>SELECT 1</validationQuery>
                  <validationInterval>30000</validationInterval>
                  </configuration>]]>
            </xsd1:dsXMLConfiguration>
            <xsd1:type>RDBMS</xsd1:type>
        </xsd1:definition>
        <xsd1:description>description</xsd1:description>
        <xsd1:jndiConfig>
            <xsd2:name>jdbc/MyCarbonDataSource</xsd2:name>
            <xsd2:useDataSourceFactory>false</xsd2:useDataSourceFactory>
        </xsd1:jndiConfig>
        <xsd1:name>MyCarbonDataSource</xsd1:name>
        <xsd1:system>false</xsd1:system>
    </xsd:dsmInfo>
</xsd:addDataSource>
   </soapenv:Body>
</soapenv:Envelope>


Following are meanings of important tags on the request


  •  <xsd1:dsXMLConfiguration: Data source configuration falls under here as a CDATA tag.
  • <url: database url
  • <Username: username used to authenticate with DB
  • <password: password of above user
  • <driverClassName: class name of the driver implementation used to connect with DB
  • <type: type of the database  (i.e RDBMS)
  • <description: description for the data-source
  • <jndiConfig<name: JNDI name by which data source should be exposed
  • <name: name of the data source

Sending the soap request by commandline


Save above soap request to a file and name it as “MyCarbonDataSource.xml”. Using curl we can send the request as below. Invoke curl command from where MyCarbonDataSource.xml exists.


curl -k -u admin:admin --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:addDataSource" --data @MyCarbonDataSource.xml https://127.0.0.1:9443/services/NDataSourceAdmin


Verifying if data source is added and active




In order to verify if the data source is added, navigate to following location using Management Console registry browser (Home>>Registry>>Browse).

/_system/config/repository/components/org.wso2.carbon.ndatasource

You will see that new data source is added to the registry



Then go to Configure >> Data Sources and verify that it is active



Now your DataSource is ready...!!







Hasitha Hiranya

No comments:

Post a Comment

Instagram