• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Datasource JNDI Lookup in WSAD from standalone app

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having problem in looking up datasource in WSAD 5.1 (not in WAS).

Following are my configurations:

Datasource tab in WSAD:
Name - DB2MYDB
JNDI Name - jdbc/MYDB
Componen-ManagedAuthentication Alias:MYDBAlias
Container-managed authentication alias: MYDBAlias

Security tab:

Alias - MYDBAlias
User ID - scott

My Java code:

public class ServerConnector {

public Connection getSARMConnection() {
Connection conn = null;
try {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
properties.put(Context.PROVIDER_URL, "iiop://localhost:9816");
Context initialContext = new InitialContext(properties);

DataSource ds = (DataSource)initialContext.lookup("DB2SARM");
//DataSource ds = (DataSource)initialContext.lookup("sarmDB");
//DataSource ds = (DataSource)initialContext.lookup("sarmDB");//DataSource ds = (DataSource)initialContext.lookup("sarmDBAlias");
//DataSource ds=
(DataSource)initialContext.lookup("java:comp/env/sarmDBAlias");
//DataSource ds = (DataSource)initialContext.lookup("jdbc/sarmDB");
//DataSource ds =
(DataSource)initialContext.lookup("java:comp/env/sarmDB")

conn = ds.getConnection();

}


}
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having problem in looking up datasource in WSAD 5.1 (not in WAS) from a stand alone app.

Following are my configurations:

Datasource tab in WSAD:
Name - DB2SARM
JNDI Name - jdbc/sarmDB
Componen-ManagedAuthentication Alias: sarmDBAlias
Container-managed authentication alias: sarmDBAlias

Security tab:

Alias - sarmDBAlias
User ID - scott

I have not used Admin console.

My Java code:

public class ServerConnector {

public Connection getSARMConnection() {
Connection conn = null;
try {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
properties.put(Context.PROVIDER_URL, "iiop://localhost:9816");
Context initialContext = new InitialContext(properties);

DataSource ds = (DataSource)initialContext.lookup("DB2SARM");
//DataSource ds = (DataSource)initialContext.lookup("sarmDB");
//DataSource ds = (DataSource)initialContext.lookup("sarmDB");
//DataSource ds = (DataSource)initialContext.lookup("sarmDBAlias");
//DataSource ds=
(DataSource)initialContext.lookup("java:comp/env/sarmDBAlias");
//DataSource ds = (DataSource)initialContext.lookup("jdbc/sarmDB");
//DataSource ds =
(DataSource)initialContext.lookup("java:comp/env/sarmDB")

conn = ds.getConnection();

}

I tried several ways to get datasource that is bound to jndi. Obviously, my WSAD Test server is running on port 9816.

Follwing are the key value pairs that I can read from JNDI:
com.ibm.websphere.naming.hostname.normalizer : com.ibm.ws.naming.util.DefaultHostnameNormalizer
java.naming.factory.initial : com.ibm.websphere.naming.WsnInitialContextFactory
com.ibm.websphere.naming.name.syntax : jndi
com.ibm.websphere.naming.namespace.connection : lazy
com.ibm.ws.naming.ldap.ldapinitctxfactory : com.sun.jndi.ldap.LdapCtxFactory
com.ibm.websphere.naming.jndicache.cacheobject : populated
com.ibm.websphere.naming.namespaceroot : defaultroot
com.ibm.ws.naming.implementation : WsnIpCos
com.ibm.ws.naming.wsn.factory.initial : com.ibm.ws.naming.util.WsnInitCtxFactory
com.ibm.websphere.naming.jndicache.maxcachelife : 0
com.ibm.websphere.naming.jndicache.maxentrylife : 0
com.ibm.websphere.naming.jndicache.cachename : providerURL
java.naming.provider.url : iiop://localhost:9816
java.naming.factory.url.pkgs : com.ibm.ws.naming:com.ibm.ws.naming

As such, I can bind to JNDI. But lookup is not successfull.

If I do lookup on sarmDB, or sarmDBAlias, DB2SARM directly (instead of java:comp/env), I get error like:

javax.naming.NameNotFoundException: Context: localhost/nodes/localhost/servers/server1, name: DB2SARM: First component in name DB2SARM not found. Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL mg.org/CosNaming/NamingContext/NotFound:1.0

If I try to look up using ava:comp/env/sarmDBAlias, I get error like:

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.

What is the correct way of looking up datasource in WSAD from Standalone app?
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
Is this in an application client project or a true standalone application outside the EAR?
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, this app is in an ear. This ear has EJB also. However, the way I am trying to lookup datasource is from a class similar to DAO (Data Access Object).
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The iiop port is 2809.Also you need not not specify these settings as 5.1 provides these settings by default so can just create the context and do the lookup.
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gavi:
I think you missed the problem statement. iiop port number can be changed. I run 2 WSAD Test servers on 2 different iiop ports to simulate remote lookup of EJBs. Default port number of 2809 can be changed.

Also, if your application runs in web container, jars are available in classpath. However, if you want to do lookup from a POJO, it requires setting up extra stuff in InitialContext.

However, I resolved my problem. I can now look up my datasource from jndi.

But it is now throing exception when I call getConnection() on that datasource.

Username and pwd are set in Security tab of WSAD configuration of that datasource. I think I am not getting connected cause of security constraint.

I get error below. Any thoughts?

Has anyone encountered Mapping the following SQLException, with ErrorCode -1,013 and SQLState 08001, to a StaleConnectionException: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] SQL1013N The database alias name or database name "SAMPLE" could not be found. SQLSTATE=42705


Pleasde note that the datasource and databse name is different from SAMPLE that I see in exception. There is no database called SAMPLE. I am connecting to DB2 database. SAMPLE may be something used by DB2 itself. Name of database schema is not SIMPLE either.
 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try switching to DB perspective and create the connection from it.
 
Jeanne Boyarsky
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
When you create a db2 DatasSOurce in WSAD , it uses the SAMPLE database by default. If you open up the server in the server perspective and go to the database tab, you can see this. Look at the properties for your datasource (at the bottom of the screen.) It should be around the second or third property. Just change this to the name of your db.

For future reference, note that we have an IBM/WebSphere forum further down. You may get faster answers there as that is where the WSAD/WAS gurus hang out.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic