Thursday, October 4, 2012

How to access Carbon data-source from a web-application using WSO2 Application Server 5.0.0

Applies To :

WSO2 Application Server 5.0.0


STEP 1 : Run RMI provider

Go to cammad line and run following command to run rmi provider (rmiregistry)
$ rmiregistry 2199


STEP 2 : Create carbon data-source

Go to "configure" tab and select  "Data Sources". 



Click on "Add Data Source" to add a new data source.

 

Add relevant configurations for the required fields  (Name, Driver, URL etc.)





















Expand "Expose as a JNDI Data Source" section and give a name and tick the "Use Data Source Factory" checkbox. And also add following JNDI properties 

Property 1

Name :  java.naming.factory.initial 
Value :  com.sun.jndi.rmi.registry.RegistryContextFactory

Property 2

Name :  java.naming.provider.url 
Value :  rmi://localhost:2199







Save the configurations.

STEP 3 : Refference Data Source from Web Application

Then you will be able to access the Data source as shown below.
Hashtable env = new Hashtable();  

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); //same context factory class provided when creating carbon data-source. 

env.put(Context.PROVIDER_URL, "rmi://localhost:2199"); //same provider url provided when creating carbon data-source. 

InitialContext ctx = new InitialContext(env);  

DataSource ds = (DataSource) ctx.lookup("jndiDS"); //"jndiDS" is the carbon data-source name that i have created. Please note that you may need to add commons-dbcp.jar and commons-pool.jar to web-app class path to do this cast.  

conn = ds.getConnection(); 





When we consider following line of code, 
DataSource ds = (DataSource) ctx.lookup("jndiDS"); 

 
You don't need to search JNDI data source as java:/comp/env/jdbc/jndiDS. You only need to search for jndiDS. This feature has been added for WSO2 Application Server 5.0.0 to increase the usability. 

5 comments:

  1. [2012-12-07 16:35:42,052] ERROR {org.wso2.carbon.ndatasource.core.DataSourceRepo
    sitory} - Error in registering data source: Sample - Error in binding to JNDI with name 'jndiDS' - null
    org.wso2.carbon.ndatasource.common.DataSourceException: Error in binding to JNDI with name 'jndiDS' - null
    at org.wso2.carbon.ndatasource.core.DataSourceRepository.registerJNDI(Da
    taSourceRepository.java:250)
    at org.wso2.carbon.ndatasource.core.DataSourceRepository.registerDataSou
    rce(DataSourceRepository.java:370)

    ReplyDelete
  2. I have followed the steps provided in the above page, still I get the Error as Above.

    Note : I have added mysql jar file to the classpath.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Just check out your RMI port that start from the server and use that port to make the url (rmi://localhost:????)
    It may be the problem of you.

    ReplyDelete
  5. WSO2 Carbon

    WSO2 Carbon redefines middleware by providing an integrated and componentized middleware platform that adapts to the specific needs of any enterprise IT project - on premise or in the cloud.

    100% open source and standards-based, WSO2 Carbon enables developers to rapidly orchestrate business processes, compose applications and develop services using WSO2 Developer Studio and a broad range of business and technical services that integrate with legacy, packaged and SaaS applications.

    The lean, complete, OSGi-based platform includes more than 175 components – OSGi bundles or Carbon features. The WSO2 Carbon core framework functions as “Eclipse for servers” and includes common capabilities shared by all WSO2 products, such as built-in registry, user management, transports, security, logging, clustering, caching and throttling services, co-ordination, and a GUI framework.

    ReplyDelete