1

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?

Java-Seekar
  • 1,720
  • 5
  • 30
  • 52
  • maybe ? http://www-01.ibm.com/support/docview.wss?uid=swg21580598 – Sean F Aug 07 '13 at 04:46
  • Thanks Seen. I tried with your solution but getting the same issue. – Java-Seekar Aug 07 '13 at 05:01
  • You cannot use the logical name in this case (java:comp...), you must use the real JNDI resource name. Check in Admin Console. – home Aug 07 '13 at 05:08
  • I used only the JNDI name then I did not get the above error but when I am creating the connection I am getting "EVERE: Cannot get connection: java.sql.SQLException: invalid arguments in callDSRA0010E: SQL State = null, Error Code = 17,433" exception. – Java-Seekar Aug 07 '13 at 06:06

2 Answers2

2

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.

Java-Seekar
  • 1,720
  • 5
  • 30
  • 52
0
DataSource dataSource = (DataSource) initialContext
                    .lookup("jdbc/oracleDS");

try this way.
look here

Philip Puthenvila
  • 512
  • 1
  • 8
  • 19