从独立应用程序访问DataSource [英] Access the DataSource from the stand alone application
问题描述
我在Websphere服务器中创建了数据源,其名称为myDataSource(JNDI名称为oracleDS)。现在我想通过独立应用程序访问它。
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.
我刚写了下面的代码来访问数据。
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.
请告诉我,我必须在websphere中进行任何配置更改才能访问数据源或任何我需要做其他代码级别的更改吗?
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)?
Update1:
我按照PhilipJ提到的方式进行操作。然后我打开了波纹管异常。
I followd the way which PhilipJ mentioned. Then I am gtting the bellow exception.
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$2.run(CNContextImpl.java:2797)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.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:
我找到了避免异常的方法。代码应该低于,
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?
推荐答案
我找到了解决这个问题的方法。
当我们创建InitialContext时,我们必须正确地设置两个环境变量,
I found the solution for this issue. When we creat the InitialContext we have to set two environment variables correctly as 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:2810");
Context initialContext = new InitialContext(pdEnv);
注意:
我们可以找到<的监听端口strong> iiop 协议使用以下方式,
Note: We can find the listening port of iiop protocol by using the bellow way,
登录Webaphere管理控制台=>服务器=>服务器类型=> Webspher应用程序服务器=>单击我们使用的服务器的名称=>通信=>端口。
Login to Webaphere admin console => Server => Server Types => Webspher application servers => Click on the name of the server which we use => Communication => Ports.
然后你可以找到端口名称 BOOTSTRAP_ADDRESS 的端口。
Then you can find the port for Port name BOOTSTRAP_ADDRESS.
使用数据源创建连接的工作代码
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.");
}
注意:我在websphere中创建了数据源(版本7)和JNDI名称 testDS
我们必须在课堂上添加 com.ibm.ws.ejb.thinclient_7.0.0.jar jar路径。
Note: I created the data source in websphere(Version 7) and the JNDI name is testDS and we have to add com.ibm.ws.ejb.thinclient_7.0.0.jar jar in the class path.
感谢Sean F和PhilipJ对此问题的支持。
Thanks Sean F and PhilipJ for your support regarding this issue.
这篇关于从独立应用程序访问DataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!