aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Datasource JNDI Lookup in WSAD from standalone app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Datasource JNDI Lookup in WSAD from standalone app" Watch "Datasource JNDI Lookup in WSAD from standalone app" New topic
Author

Datasource JNDI Lookup in WSAD from standalone app

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
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

Joined: Jul 21, 2003
Posts: 281
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?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30776
    
157

Sam,
Is this in an application client project or a true standalone application outside the EAR?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
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).
Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
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

Joined: Jul 21, 2003
Posts: 281
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

Joined: Apr 28, 2005
Posts: 82
Try switching to DB perspective and create the connection from it.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30776
    
157

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Datasource JNDI Lookup in WSAD from standalone app