Sunday, June 23, 2013

How to create a MySQL database and a datasource using WSO2 Appfactory


With the beta release of WSO2 App factory, it enables you to create a database and datasource for the applications you create using App Factory.
Current latest version supports MySQL.
This blog post explains you steps to create a database and a datasource and how to point them in a Web application (WAR) created using WSO2 App Factory.

For more information on WSO2 App Factory :



To create a WAR/ Web app supports the datasource

  1. Registered user can successfully login to WSO2 app factory and create a web application. As an example I will be creating a web application named ColourFindApp.


    Application Creation

  2. Once I created the application, I will be assigning user roles for the application using the Team page.
  3. Then a developer or the app owner which has developer rights will be checking out the source code using GIT and do the necessary code level changes via Developer studio and then commit the code.
    http://ushanib.blogspot.com/2013/02/steps-to-checkout-and-commit-wso2.html
  4. As an example assume in my source code, I refer my datasource as colour. So the sample code in my application source code will be something like below.
    E.g.,

    …............
      Hashtable hashtable = new Hashtable();  
 hashtable.put("java.naming.factory.initial","org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory");  
 Context initContext = new InitialContext(hashtable);  
 ds = (DataSource) initContext.lookup("jdbc/colour");  
 Connection conn = ds.getConnection();  
 Statement statement = conn.createStatement();   
        …............

  1. So the datasource I will be creating should be named as colour.
Databases Permission level

User Role Permission
App Owner Can create and configure databases in Development, Testing and Staging
Developer Can create and configure databases in Development and Testing environments
QA/Tester Can create and configure databases in Testing and Staging environments
Dev-Ops Can create and configure databases in Staging and Production environments
  1. A user can login to the system and select the resource section to create a database.
  2. User can create the database as per the above permission. Visibility of Database environment will be as above user role permission. User will be able to select the environment from the drop down. DB User and template can be created inline or separately and select it form the drop down.
  3. Created users and the templates for each environment will be loaded depends on the selected database environment.


                                              Database Creation


    Datasource Permission Level
User Role Permission
App Owner Can create and edit datsources in Development, Testing and Staging
Developer Can create and edit datasources in Development and Testing environments
QA/Tester Can configure created datasources in Testing and Staging environments
Dev-Ops Can configure created datadources in Staging and Production environments
  1. App owner can create a datasource and provide the database url created initially. E.g., I create a database named COL_DB and Datasource as colour.
  2. Then a datasource will be copied in to testing and staging as well along with the provided database URL. E.g., If I provide the database URL of the development stage it will copy the database URL of development stage in testing and staging as well.
    E.g.,
    jdbc:mysql://rss.dev.appfactorypreview.wso2.com:3306/COL_DB

                                Shows the datasources visibility as an APP Owner

  3. If a developer logs in to the system, he or she will see the datasources created in Dveloement and testing stages.
  4. A Developer or a testing user can create another database in testing environment and select the datasource created and the stage as Testing and provide the newly created database URL of the testing stage. E.g., Database created in testing : COL_DB in testing storage server and select the colour as the datasource and the stage as testing and the testing DB URL and update it.

    E.g.,
    jdbc:mysql://rss.test.appfactorypreview.wso2.com:3306/COL_DB
  5. This will enable the different users to easily update their datasource with their desired database location and test the application.
  6. Depends on the stage selected, existing database URL and attached users will be loaded.
  7. If not a developer or a tester can test their application with the datasource created initially which points to the database in the development without editing and configuring it to the different environment.
  8. When promoting the application in to proceeding stage it will refer the database pointed to the promoted environment.
  9. Only Dev Ops have permission to configure the production environment. Therefore DevOps will have to select the stage as Production and provide the database URL accordingly.