Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

datasource jndi cast error

 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help. I am using websphere 5, and MySQL as my database. I created a datasource in my websphere server test environment. I create a simple java application to lookup the datasource.
Here is my code :
public static void main(String arg[]){
try{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/ds1");
System.out.println("Finished");
}catch(Exception e){
e.printStackTrace();
}
}
An error is occuring on DataSource cast. I dont know why..
Here is the error :
java.lang.ClassCastException: javax.naming.Reference
at ds.test.DsTestMain.main(DsTestMain.java:37)
Hope someone can help me.
 
Manish Pawar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two possible reasons for above, which i cn think of at present..
- either the lookup is not happening.
For this run - (WAS_Install_Root)/AppServer/bin/dumpNameSpace - to check whether the name -"jdbc/ds1" exist or not. If it doesn't, the name has not yet been 'configured' yet since u hv created the datasource. Check the configurations for datasource, if they are all right, restart the websphere for changes to take effect.
- or the casting is improper.

One possible solution is to narrow down the object first and then use casting. (Use PortableRemoteObject.narrow )
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ajsc ,
Welcome to JavaRanch, the absolute best site on the www for Java information. We don't have many rules around here, but we do have one. Please change your display name to a first and last name to comply with the JavaRanch Naming Policy. You can change it here. Thank you for your cooperation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic