File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Websphere and the fly likes Error while getting Datasource object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark "Error while getting Datasource object" Watch "Error while getting Datasource object" New topic
Author

Error while getting Datasource object

Jyoti Sankar Padhi
Greenhorn

Joined: May 27, 2010
Posts: 5
Hi,

I have created a data source in web sphere console and now I am trying to get a database connection using this data source. I am getting exception while looking for the JNDI name of the data source.

Below are the code and exceptions

Code: -

DataSource ds = null;
Connection conn = null;
try{
Context context = new InitialContext();
//InitialContext context = new InitialContext();

ds = (DataSource)context.lookup("jdbc/XADataSource");
if (ds != null) {
conn = ds.getConnection();
System.out.println("Connected:");
}
}



Exception -



javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:653)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:257)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:294)
at javax.naming.InitialContext.lookup(InitialContext.java:361)
at com.metlife.onlinepolicy.DBConnection.getConnection(DBConnection.java:30)
at com.metlife.onlinepolicy.DBConnection.main(DBConnection.java:47)



Code: -

Hashtable env = new Hashtable();
DataSource ds = null;
Connection conn = null;

try{

env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL,"iiop://localhost:2809");
Context context = new InitialContext(env);
ds = (DataSource)context.lookup("jdbc/XADataSource");

if (ds != null) {
conn = ds.getConnection();
System.out.println("Connected:");
}
}


Exception:-

May 27, 2010 12:31:13 PM com.ibm.ws.naming.util.CommonHelpers
WARNING: jndiproviderProperties
May 27, 2010 12:31:15 PM com.ibm.ws.naming.util.CommonHelpers
WARNING: jndiproviderProperties
May 27, 2010 12:31:15 PM com.ibm.websphere.naming.WsnInitialContextFactory
WARNING: noInitCtxFactoryImpl
javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.
at com.ibm.websphere.naming.WsnInitialContextFactory.init_implClassCtor(WsnInitialContextFactory.java:194)
at com.ibm.websphere.naming.WsnInitialContextFactory.getInitialContext(WsnInitialContextFactory.java:112)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:675)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:257)
at javax.naming.InitialContext.init(InitialContext.java:233)
at javax.naming.InitialContext.<init>(InitialContext.java:209)
at com.metlife.onlinepolicy.DBConnection.getConnection(DBConnection.java:24)
at com.metlife.onlinepolicy.DBConnection.main(DBConnection.java:47)

Please help.

Thanks,
J S Padhi

Laxman Nagalla
Greenhorn

Joined: Feb 01, 2008
Posts: 10
The way your righting is correct. Can you check with this

Step 1:- Check jdbc/XADataSource Test connectivity is fine in WAS Admin console
Step 2:- If Test Connectivity is pass then in your code change ds = (DataSource)context.lookup("jdbc/XADataSource"); as below
ds = (DataSource)context.lookup("java:comp/env/jdbc/XADataSource"); //the error shows environment needed

Plese let me know if it works


Thanks & Regards<br />Laxman N
Jyoti Sankar Padhi
Greenhorn

Joined: May 27, 2010
Posts: 5
Hi Laxman,

I tried with
ds = (DataSource)context.lookup("java:comp/env/jdbc/XADataSource");

but still I am getting the same exception.

May 31, 2010 6:10:24 PM com.ibm.ws.naming.util.CommonHelpers
WARNING: jndiproviderProperties
May 31, 2010 6:10:30 PM com.ibm.ws.naming.util.CommonHelpers
WARNING: jndiproviderProperties
May 31, 2010 6:10:30 PM com.ibm.websphere.naming.WsnInitialContextFactory
WARNING: noInitCtxFactoryImpl
javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.
at com.ibm.websphere.naming.WsnInitialContextFactory.init_implClassCtor(WsnInitialContextFactory.java:194)
at com.ibm.websphere.naming.WsnInitialContextFactory.getInitialContext(WsnInitialContextFactory.java:112)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:675)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:257)
at javax.naming.InitialContext.init(InitialContext.java:233)
at javax.naming.InitialContext.<init>(InitialContext.java:209)
at com.metlife.onlinepolicy.DBConnection.getConnection(DBConnection.java:25)
at com.metlife.onlinepolicy.DBConnection.main(DBConnection.java:47)
Jyoti Sankar Padhi
Greenhorn

Joined: May 27, 2010
Posts: 5
The code is working fine if I access the ds from a servlet. When I want to run the code from main method this is giving error.
 
 
subject: Error while getting Datasource object