• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problem in lookup Oracle DataSource in WAS 7 from standalone Java program

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help stuck on this issue for long and we have a production deployment very soon so need your help.
Actually we are moving our standalone java program from Weblogic to Websphere 7.0 and when i was trying to run the below program which connects to datasource created in Websphere through jndi lookup then i am getting the ClasscastException.


java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.sql.DataSource

This is the code snippet i am trying to run


Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
ht.put(Context.PROVIDER_URL, PROVIDER_URL+ "/");

//ht.put("org.omg.CORBA.ORBClass", "com.ibm.CORBA.iiop.ORB");
ctx = new InitialContext(ht);
Object obj = ctx.lookup(JNDINAME);
System.out.println("CTX Obj " + obj);
System.out.println("Context Lookup "+ ctx.PROVIDER_URL);
// javax.sql.DataSource ds = (javax.sql.DataSource)javax.rmi.PortableRemoteObject.narrow(ctx.lookup(JNDINAME), javax.sql.DataSource.class);
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup(JNDINAME);
System.out.println("Datasource "+ds);
connection = ds.getConnection();
System.out.println("Connection "+connection.getMetaData().getURL());



java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.sql.DataSource

 
Umesh K Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help, it looks to me the problem due to server specific jars.

Any help will be appreciated.
 
Bartender
Posts: 3727
39
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would try the following:

1) ctx.lookup(...) - is it performed OK, if yes - that's great, means it can access JNDI and there is some object by that key.
2) assign what returned by ctx.lookup(...) to Object variable (what it actually has defined in API).
3) In debugger - check what Object variable refers to and understand better.

In general, when using RMI you need to perform narrowing, not casting. Seems this explained in your class cast exception. Also, I see in your code a line for narrowing, but it was commented out.

Casting can work IIRC only when used local view, not remote view.

HTH,
MZ
 
Umesh K Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mikalai for reply

I updated the code with narrow and getting this output, can you please help

Jun 4, 2015 7:48:56 AM com.ibm.ws.security.config.SecurityObjectLocator
INFO: Client code attempting to load security configuration
Jun 4, 2015 7:48:59 AM com.ibm.ws.naming.util.Helpers
WARNING: jndiGetObjInstNoop
CTX Obj---------------------->>>> Reference Class Name: javax.resource.cci.ConnectionFactory
Type: connectorName
Content: AwsotsDataSource
Type: classpath
Content: /usr/local/opt/was/was70/lib/rsadapter.rar:/usr/local/opt/was/was70/lib/rsadapter.rar
Type: FactoryJndiName
Content: jdbc/awsots
Address Type: dd
AddressContents: ffffffac ffffffed 0 5 73 72 0 25 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 52 65 73 6f 75 72 63 65 ...
Address Type: miscProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 14 6a 61 76 61 2e 75 74 69 6c 2e 50 72 6f 70 65 72 74 69 65 73 39 12 ffffffd0 7a ...
Address Type: poolProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 27 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 43 6f 6e 6e 65 63 74 6f ...
Address Type: configProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 23 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 43 6f 6e 6e 65 63 74 6f ...
Address Type: mmProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 14 6a 61 76 61 2e 75 74 69 6c 2e 50 72 6f 70 65 72 74 69 65 73 39 12 ffffffd0 7a ...
Address Type: dsProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 14 6a 61 76 61 2e 75 74 69 6c 2e 50 72 6f 70 65 72 74 69 65 73 39 12 ffffffd0 7a ...
Address Type: mcfProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 2d 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 4d 61 6e 61 67 65 64 43 ...
Address Type: mbeanProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 1a 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 4d 42 65 61 6e 50 72 6f ...

CLASSLOADER ----->>> nulljavax.sql.DataSource.class--->> null
Jun 4, 2015 7:48:59 AM com.ibm.ws.naming.util.Helpers
WARNING: jndiGetObjInstNoop
Exception in thread "P=135513:O=0:CT" java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.portable.ObjectImpl
at com.ibm.rmi.javax.rmi.PortableRemoteObject.getObjectImpl(PortableRemoteObject.java:587)
at com.ibm.rmi.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:326)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:135)
at Test.main(Test.java:37)
 
Umesh K Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i am getting this exception

java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.portable.ObjectImpl
 
Mikalai Zaikin
Bartender
Posts: 3727
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the datasource working fine? Does the test pass in web admin console (ISC)
Is the JNDI name in code matches to datasource JNDI name?
 
Mikalai Zaikin
Bartender
Posts: 3727
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Umesh K Sharma wrote:Now i am getting this exception

java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.portable.ObjectImpl



Show the screenshot of JNDI name configured in WAS admin console and used in your code.
 
Umesh K Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that are correct. Do you think that it could be any jars issue which should be app server specific?
 
Mikalai Zaikin
Bartender
Posts: 3727
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Umesh K Sharma wrote:Yes that are correct. Do you think that it could be any jars issue which should be app server specific?



I'd love to see datasource definition, JDBC provider definition. Plus WAS log file on startup - datasource specific part will be enough, ir grep by "JNDI" keyword.

Another hint - try to reconfigure same JNDI name to other JDBC provider (like DB2 which is more common) to connect to some DB2 instance.

Then you can track if this is a server side or client side issue.
 
Umesh K Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And as per exception also seems to be that it is able to lookup up the jndi succesfully but when the return object is trying to cast with javax.sql.Datasource then it is throwing exception : java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.portable.ObjectImpl

May be i am wrong.
 
Bartender
Posts: 1314
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait a moment: are you trying to lookup a Datasource from standalone Java program , as you stated in your title ? I think that such a thing is simply impossible. While you may look up a remote EJB interface - and client runtime, generally speaking strictly coupled with server runtime, will handle this for you using the right stub - I don't think that a datasource may be looked up from a standalone client, not only the way you are tryng, but in general. If you succeeded, you may use getConnection() method on datasource and get a Connection. How ? Should the remote server open the connection and pass it over TCP/IP ? It simply can't, Connections aren't serializable.
Let me understand better this.
 
Mikalai Zaikin
Bartender
Posts: 3727
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claude Moore wrote:Wait a moment: are you trying to lookup a Datasource from standalone Java program , as you stated in your title ? I think that such a thing is simply impossible. While you may look up a remote EJB interface - and client runtime, generally speaking strictly coupled with server runtime, will handle this for you using the right stub - I don't think that a datasource may be looked up from a standalone client, not only the way you are tryng, but in general. If you succeeded, you may use getConnection() method on datasource and get a Connection. How ? Should the remote server open the connection and pass it over TCP/IP ? It simply can't, Connections aren't serializable.
Let me understand better this.



Hi Claude,

You think or you know?

Check my older WAS exam guide - it's absolutely possible (although a rare scenario)

Hint: In order to perform JNDI lookups on remote WAS you must have Application client container installed on Java client side.

regards,
MZ
 
Claude Moore
Bartender
Posts: 1314
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:

Hi Claude,

You think or you know?

Check my older WAS exam guide - it's absolutely possible (although a rare scenario)

Hint: In order to perform JNDI lookups on remote WAS you must have Application client container installed on Java client side.



Sorry Mikalai, I should have been more precise: I think, but I don't know it for sure, that this kind of remote lookup may not be done.
The page you linked shows remote lookup of EJBs, which is absolutely possible, as long as you have specific application client runtime installed (as you wrote), and I tried it myself a couple of times succesfully.
But with remote EJBs, are EJBs that actually look up datasources, handle connections, do things and so on. On the server side. Client has only stubs (or a proxy interfaces) to work with, no visibilty of datasources involved.
And as far as I understand, the OP asked how to lookup datasources, not remote EJBs instances.

 
Claude Moore
Bartender
Posts: 1314
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Googling a bit, it seems that JBoss AppServer supported this feature some version ago but now it's no longer supported, just look at this topic. Of course we're all talking about another appserver - not WebSphere. So, it was technically possible (even if not reccomended).
 
Mikalai Zaikin
Bartender
Posts: 3727
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claude Moore wrote:Googling a bit, it seems that JBoss AppServer supported this feature some version ago but now it's no longer supported, just look at this topic. Of course we're all talking about another appserver - not WebSphere. So, it was technically possible (even if not reccomended).



It's possible to get a DataSource in a standalone application via JNDI from remote WAS, please take a look

(Although I must mention again: it's pretty rare scenario)

HTH,
MZ
 
Claude Moore
Bartender
Posts: 1314
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's something I did not know and I would have bet that was unfeasible. So, thank you for sharing that
A question arise, however: why should one lookup a remote datasource from an application client ? Wouldn't be more logic (and safe from a transactional integrity point of view, and more performant) to expose a public interface of some EJB (maybe, not via RMI but via REST or SOAP ) ?
 
Umesh K Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes in WAS it is possible through IIOP to access remotely the datasource. And if you look at the exception, it is clear that it is succefully lookup the JNDI but when it was trying to cast the return object into Javax.sql.Datasource it is throwing exception.

Can someone have seen this scenario and help me to resolve.
 
Mikalai Zaikin
Bartender
Posts: 3727
39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Umesh K Sharma wrote:Yes in WAS it is possible through IIOP to access remotely the datasource. And if you look at the exception, it is clear that it is succefully lookup the JNDI but when it was trying to cast the return object into Javax.sql.Datasource it is throwing exception.

Can someone have seen this scenario and help me to resolve.



Google says that it might be caused by some WAS JARs missing in CLASSPATH. So, code might be fully correct and not require changes.

I'd suggest you to :
  • Look at %WAS_HOME%\bin\launchClient.bat - I hope you're using it for launching your client ?
  • Try to add all JARs from %WAS_HOME%\lib to client's CLASSPATH for safety.
  •  
    Umesh K Sharma
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot Mikalai Zaikin. I am able to resolve this issue. what i did was just put all the jars present in WAS LIB and PLUGIN to my client classpath and it got worked for me.

    Thanks all whoever has given me input to resolve this issue. Special thanks to Mikalai
     
    Mikalai Zaikin
    Bartender
    Posts: 3727
    39
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Umesh K Sharma wrote:Thanks a lot Mikalai Zaikin. I am able to resolve this issue. what i did was just put all the jars present in WAS LIB and PLUGIN to my client classpath and it got worked for me.



    Yup, this is what I expected.

    The program compiles and executes, but gives wrong result if CLASSPATH does not contain some specific WAS libraries.

    Glad to now you managed to get it fixed.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic