I wanted to create a database connection using the Carbon datasource in java app. I'm getting the below exception
javax.naming.NameNotFoundException: jdbc at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:116) at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:124) at javax.naming.InitialContext.lookup(InitialContext.java:411) at org.wso2.ApiInvocationHnadler.DBConnection.testJndiDataSource(DBConnection.java:77) at org.wso2.ApiInvocationHnadler.ApiInvocationHandler.handleRequest(ApiInvocationHandler.java:35) at org.apache.synapse.rest.API.process(API.java:284) at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:83) at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:64) at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:220) at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.processRESTRequest(MultitenantMessageReceiver.java:558) at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.doNhttpREST(MultitenantMessageReceiver.java:498) at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.doREST(MultitenantMessageReceiver.java:317) at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.processRequest(MultitenantMessageReceiver.java:210) at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.receive(MultitenantMessageReceiver.java:77) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:344) at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:168) at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)
here is the 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>
Java Code
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");
How do I over come this issue?