Labels

Wednesday, February 4, 2015

How to access Carbon data-source from master-datasources.xml WSO2

This post is related to getting carbon datasource from master-datasource.xml file.

Accessing the carbon data-source is pretty much easy all you have to do is use Lookup to read the datasource


master-datasources.xml
 <datasource>  
             <name>test_db</name>  
             <description>The datasource used for </description>  
             <jndiConfig>  
                     <name>jdbc/test_db</name>  
             </jndiConfig>  
             <definition type="RDBMS">  
                     <configuration>  
                             <url>jdbc:mysql://localhost:3306/test_db?autoReconnect=true</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>  
             </definition>  
 </datasource>  


When getting the master-datasource,we should do the lookup from carbon super tenant.
If currently in the carbon super tenant there are two ways to get the datasource 

but the Second way is much easier.

    Hashtable env = new Hashtable();  
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");  
    env.put(Context.PROVIDER_URL, "rmi://localhost:2199");  
    InitialContext ctx = new InitialContext(env);  
    DataSource ds = (DataSource) ctx.lookup("jdbc/test_db");  

E.g. 2
 dataSource = (DataSource) InitialContext.doLookup("jdbc/test_db");  

If not in carbon super tenant, you should switch the tenant flow and get the datasource and switch it back.
(If you know any other easy way feel free to put a comment  :) )
  //super tenant credentials  
       int tenantId= MultitenantConstants.SUPER_TENANT_ID;  
       String tenantDomain=MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;  
       //changing the tenant flow to the supper tenant  
       Connection conn=null;  
       try{  
         PrivilegedCarbonContext.startTenantFlow();  
         PrivilegedCarbonContext privilegedCarbonContext =    PrivilegedCarbonContext.getThreadLocalCarbonContext();  
         privilegedCarbonContext.setTenantId(tenantId);  
         privilegedCarbonContext.setTenantDomain(tenantDomain);  
         //getting the cloud-mgt datasource connection  
         DataSource ds = (DataSource) privilegedCarbonContext.getJNDIContext().lookup("jdbc/test_db");  
         conn = ds.getConnection();  
       } catch (NamingException e) {  
          log.error("Error while getting the DataSource" +e);  
          e.printStackTrace();  
       } catch (SQLException e) {  
          e.printStackTrace();  
       } finally {  
          //Ending the tenant flow  
          PrivilegedCarbonContext.endTenantFlow();  
          return conn;  
       }  

pretty simple hope this post help you,

Have fun friends.