Search WebSpherePower's 6,962 WebSphere, Java, and Eclipse article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Twitter Feed Click here for the Twitter feed.
Locate your JDBC DataSource in WebSphere Application Server using JNDI (continued)

According to the WebSphere Application Server 5.1 Information Center at http://publib.boulder.ibm.com/infocenter/ws51help/index.jsp?topic=/com.ibm.Websphere.base.doc/info/aes/ae/rnam_example_prop1.html:

"The WebSphere Application Server initial context factory, com.ibm.Websphere.naming.WsnInitialContextFactory, is typically used by WebSphere Application Server applications to perform JNDI operations. The WebSphere Application Server run-time environment is set up to use this WebSphere Application Server initial context factory if one is not specified explicitly by the JNDI client."

Based on that information, and my own experience that you can leave the WebSphere-specific initial context factory parameter out and still obtain correct results, I'd say that explicitly naming the initial context factory is redundant. It doesn't hurt anything to throw that in, though, and that is the recommended procedure.

However, that is not the source of the problem in locating your DataSource. The problem is in the way that you specify the DataSource name.

The Easy Fix
The simplest way to correct this problem is just to strip out all of the prefix information and refer to your DataSource directly by the JNDI name (jdbc/myDataSource):

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.Websphere.naming.WsnInitialContextFactory");
InitialContext ctx = new InitialContext(env);
DataSource ds = (DataSource) ctx.lookup("jdbc/myDataSource");

WebSphere supports such direct JNDI references to JDBC DataSources, but this does result in a rather annoying little problem. If you look into your server log, you will now see a number of logged messages that look something like this:

I J2CA0122I: Resource reference jdbc/myDataSource could not be located, so default values of the following are used: [Resource-ref settings]

res-auth: 1 (APPLICATION)
res-isolation-level: 0 (TRANSACTION_NONE)
res-sharing-scope: true (SHAREABLE)
res-resolution-control: 999 (undefined)

I J2CA0107I: Component-managed authentication alias not specified for connection factory or DataSource myDataSource.

Now, these messages are completely harmless, and will not prevent your application from functioning correctly; however, if you want to get rid of them, you'll have to go to the extra trouble of performing the look-up using the recommended procedure.

The Preferred Method
The method recommended by IBM is to use a resource alias to locate your JDBC DataSource rather than specifying the JNDI name directly. This actually looks a little closer to the old, familiar method, although you don't see the jdbc portion of the name:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.Websphere.naming.WsnInitialContextFactory");
InitialContext ctx = new InitialContext(env);
DataSource ds = (DataSource) ctx.lookup("java:comp/env/myDsAliasName");


« Previous  ·  1  ·  2  ·  3  ·  Next »
Other articles you might like
Home > Java (6 articles)
   Use WebSphere to develop Java applications for Palm handhelds
   A simple introduction to J2EE
   Accessing remote AS/400 databases using JTOpen
Home > Database Integration (3 articles)
   Creating a MySQL data source using WebSphere Application Server
   Easily connecting to MySQL databases from WebSphere Studio
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
Recent WebSpherePower Articles
A perfect 10: celebrating 10 years online
You can help bring security and safety back to White House email
Introducing the WebSpherePower RSS feeds
From New Jersey to Palm Bay, Florida
A WebSphere pot o' gold
How Elvis entered the building and CES went out the window
WebSphere Application Server 6: what's it all mean?
WebSpherePower News
Excitement brewing for JavaOne 2010, with or without Google
Large companies ignore data centre advice
Onapsis to Release ERP Vulnerability Testing Suite
Botnet Takedown May Yield Valuable Data
VMware app dev platform gazes beyond SpringSource Java
IBM Claims World's Fastest Chip
'Free Java': InfoWorld's guide to the protest goodies
>> Read all the news
More from the ZATZ journals
Computing Unplugged: Smartphone smarts for a mobile world
David Gewirtz Online: CNN commentary and analysis
DominoPower: It's time for Lotus to double-down on Linux and open source
OutlookPower: The strange case of Outlook losing notes and requiring passwords
ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login