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.
JDBC DATASOURCE IN WEBSPHERE
Locate your JDBC DataSource in WebSphere Application Server using JNDI
By Jeff Chilton

"Success is simple. Do what's right, the right way, at the right time." -- Arnold H. Glasgow (American Psychologist)

When they start using the WebSphere Application Server, most developers who have worked in application server environments other than WebSphere usually struggle a bit when attempting to locate their JDBC DataSources. Although both the process and syntax are similar enough to appear quite familiar, they're just different enough to prevent success without a little minor tweaking from what works in other environments.

The Familiar Method
If you've done any work with JDBC DataSources in a Web application, you already understand the basic process: create an initial JNDI (Java Naming and Directory Interface) context, then use the context to perform a "lookup" to retrieve your DataSource. In Java code, this usually looks something like this (for simplicity's sake, the requisite try/catch block surrounding these statements has been removed):

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDataSource");

While this looks great, and conforms to the established conventions used by most Web application servers, in a WebSphere Application Server this code will invariably result in the dreaded javax.naming.NameNotFoundException. To correctly locate your DataSource, you have two options, the first of which is a little simpler, but will result in some harmless messages cluttering up your server log file.

The WebSphere-specific Initial Context Factory
According to most IBM documentation and articles, the first thing you need to do when establishing an initial context is to set up the environment. The conventional code suggested for this operation looks something like this:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.Websphere.naming.WsnInitialContextFactory");
InitialContext ctx = new InitialContext(env);

This works great, although I must admit, I'm not sure exactly what this buys you. I run a lot of open source code on WebSphere that was originally written for other environments; most such code does not contain this set-up process and it all seems to work fine as long as the DataSource name parameter is correct.


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
-- Advertisement --

How To Save Jobs
This book is about how to create and save jobs. Believe it or not, there's not a single book out there that specifically focuses on job creation and preservation -- until now.

This book, by ZATZ editor-in-chief David Gewirtz, is about helping your business work better. It's about helping you change the things you need to change so your company can perform more effectively.

Plus, through a grant from ZATZ, it's a free download.

Read it and reap.

-- Advertisement --

Influencer. Recommender. Decision Maker.
They all read WebSpherePower Magazine. They all rely on WebSpherePower Magazine.

If you want to reach the inner-circle of IBM IT professionals, you won't find a better resource than WebSpherePower Magazine.

Click for our Media Kit

ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login