Java 从独立应用程序访问数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18094822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:20:24  来源:igfitidea点击:

Access the DataSource from the stand alone application

javajakarta-eewebspheredatasource

提问by Java-Seekar

I have created the data source in Websphere server and its name is myDataSource(JNDI name is oracleDS). Now I want to access it through the stand alone application.

我已经在 Websphere 服务器中创建了数据源,它的名称是 myDataSource(JNDI 名称是 oracleDS)。现在我想通过独立应用程序访问它。

I just wrote the bellow code to access the data sorce.

我刚刚编写了以下代码来访问数据源。

Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");

Context initialContext = new InitialContext(pdEnv);

DataSource dataSource = (DataSource) initialContext
                    .lookup("java:comp/env/jdbc/oracleDS");

But when I run the application I am getting the bellow exception.

但是当我运行应用程序时,我收到了以下异常。

Aug 6, 2013 11:33:41 PM null null
SEVERE: javaAccessorNotSet
javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment.

Please let me know do I have to do any configuration changes in websphere to access the data source or any other code level changes do I have to do?

请让我知道我是否必须在 websphere 中进行任何配置更改才能访问数据源或我必须进行任何其他代码级别更改?

Please confirm me can we access the datasource from outside of the container(websphere)?

请确认我可以从容器(websphere)外部访问数据源吗?

Update1:

更新1:

I followd the way which PhilipJ mentioned. Then I am gtting the bellow exception.

我遵循了 PhilipJ 提到的方式。然后我得到了波纹管例外。

SEVERE: Cannot get connection: javax.naming.NameNotFoundException: Context: LTC-VIRTUS-24WZNode01Cell/nodes/LTC-VIRTUS-24WZNode01/servers/server1, name: jdbc/oracleDS: First component in name oracleDS not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
javax.naming.NameNotFoundException: Context: LTC-VIRTUS-24WZNode01Cell/nodes/LTC-VIRTUS-24WZNode01/servers/server1, name: jdbc/oracleDS: First component in name oracleDS not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
    at com.ibm.ws.naming.jndicos.CNContextImpl.mapNotFoundException(CNContextImpl.java:4365)
    at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1794)
    at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1749)
    at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt(CNContextImpl.java:1500)
    at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:637)
    at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:165)
    at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
    at javax.naming.InitialContext.lookup(InitialContext.java:436)
    at com.nyl.connection.ConnectionUtil.getConnection(ConnectionUtil.java:38)
    at com.nyl.main.Main.main(Main.java:9)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
    at org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(NotFoundHelper.java:95)
    at com.ibm.WsnOptimizedNaming._NamingContextStub.resolve_complete_info(_NamingContextStub.java:506)
    at com.ibm.ws.naming.jndicos.CNContextImpl.run(CNContextImpl.java:2797)
    at com.ibm.ws.naming.jndicos.CNContextImpl.run(CNContextImpl.java:2793)
    at com.ibm.ws.naming.util.CommonHelpers.retry(CommonHelpers.java:763)
    at com.ibm.ws.naming.jndicos.CNContextImpl.cosResolve(CNContextImpl.java:2791)
    at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1790)
    ... 8 more

Update2:

更新2:

I found the way to avoid the exception. The code should be bellow,

我找到了避免异常的方法。代码应该如下

Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");

Context initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext.lookup("oracleDS");

if (datasource != null) {
connection = datasource.getConnection("admin","admin");
} else {
LOGGER.info("Failed to lookup datasource.");
}

But the problem here is I am giving the database credintials to creat the connection. I don't want to give it. Can any one please let me know how to create the connection without giving the database credintials?

但这里的问题是我给数据库凭据来创建连接。我不想给。任何人都可以让我知道如何在不提供数据库凭据的情况下创建连接吗?

采纳答案by Java-Seekar

I found the solution for this issue. When we creat the InitialContext we have to set two environment variables correctly as bellow,

我找到了这个问题的解决方案。当我们创建 InitialContext 时,我们必须正确设置两个环境变量,如下所示,

Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory"); 
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2810");

Context initialContext = new InitialContext(pdEnv);

Note:We can find the listening port of iiopprotocol by using the bellow way,

注意:我们可以通过下面的方式找到iiop协议的监听端口,

Login to Webaphere admin console => Server => Server Types => Webspher application servers => Click on the name of the server which we use => Communication => Ports.

登录到 Webaphere 管理控制台 => 服务器 => 服务器类型 => Webspher 应用程序服务器 => 单击我们使用的服务器的名称 => 通信 => 端口。

Then you can find the port for Port name BOOTSTRAP_ADDRESS.

然后您可以找到端口名称BOOTSTRAP_ADDRESS的端口。

Working code to create the connection using the datasource

使用数据源创建连接的工作代码

Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");             
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2810");

Context initialContext = new InitialContext(pdEnv);
DataSource datasource = (DataSource) initialContext.lookup("testDS");

if (datasource != null) {
  connection = datasource.getConnection("admin","admin"); // DB credintials
} else {
  LOGGER.info("Failed to lookup datasource.");
}

Note:I created the data source in websphere(Version 7) and the JNDI name is testDSand we have to add com.ibm.ws.ejb.thinclient_7.0.0.jarjar in the class path.

注意:我在 websphere(Version 7) 中创建了数据源,JNDI 名称是testDS,我们必须在类路径中添加com.ibm.ws.ejb.thinclient_7.0.0.jarjar。

Thanks Sean F and PhilipJ for your support regarding this issue.

感谢 Sean F 和 PhilipJ 对此问题的支持。

回答by Philip Puthenvila

DataSource dataSource = (DataSource) initialContext
                    .lookup("jdbc/oracleDS");

try this way.
look here

试试这个方法。
看这里