从 master-datasources.xml 访问 Carbon 数据源
Access Carbon datasource from master-datasources.xml
我想在 java 应用程序中使用 Carbon 数据源创建数据库连接。
我收到以下异常
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.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)
这里是高手-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代码
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");
我该如何解决这个问题?
我想指出我用来创建该数据源的代码。您可以从 here 中找到它。只需使用默认构造函数创建 InitialContext
,我就可以从 master-datasources.xml
文件加载数据源。
dataSource = (DataSource) InitialContext.doLookup(dataSourceName);`
问题是由于租户流引起的,必须获取超级租户流并查找数据源
我想在 java 应用程序中使用 Carbon 数据源创建数据库连接。 我收到以下异常
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.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)
这里是高手-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代码
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");
我该如何解决这个问题?
我想指出我用来创建该数据源的代码。您可以从 here 中找到它。只需使用默认构造函数创建 InitialContext
,我就可以从 master-datasources.xml
文件加载数据源。
dataSource = (DataSource) InitialContext.doLookup(dataSourceName);`
问题是由于租户流引起的,必须获取超级租户流并查找数据源