Access the DataSource from the stand alone application

徘徊边缘 提交于 2019-12-11 09:39:50

问题


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.

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?

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

Update1:

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?


回答1:


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);

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

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

Then you can find the port for Port name 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 testDS and we have to add com.ibm.ws.ejb.thinclient_7.0.0.jar jar in the class path.

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




回答2:


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

try this way.
look here



来源:https://stackoverflow.com/questions/18094822/access-the-datasource-from-the-stand-alone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!