• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Error while getting Datasource object

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jyoti Sankar Padhi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic