Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

instanceof strange behaviour

Thread view: 
Luís Amorim - 15 Sep 2006 17:31 GMT
Hi there.
I have a problem that consists of the following.

In my class, I have a method named getNativePreparedStatement that
receives a parameter of the type PreparedStatement:
private PreparedStatement getNativePreparedStatement(PreparedStatement
ps)

I pass to this method a non null object of the type:
org.apache.commons.dbcp.DelegatingPreparedStatement

Inside this method, I do a conditional instruction like this:
if(ps instanceof DelegatingPreparedStatement){
  //some code here
}

What happens is that in the if instruction the instanceof operator does
not recognize the object ps as a DelegatingPreparedStatement object.
Now, I am sure ps is of the type DelegatingPreparedStatement.

Does anyone have some clue why this happens?
I am running this application under the Apache Tomcat application
server and I am using struts.

Regards,
LA
yogi - 15 Sep 2006 20:27 GMT
how does PreparedStatement and DelegatingPreparedStatement relate to
each other... is it subclass of PreparedStatement , or
PreparedStatement  is a interface and DelegatingPreparedStatement
implements it... then in that case it would be ok...

> Hi there.
> I have a problem that consists of the following.
[quoted text clipped - 22 lines]
> Regards,
> LA
Jean-Francois Briere - 15 Sep 2006 20:32 GMT
> ps instanceof DelegatingPreparedStatement is false

Must have something to do with multiple class loaders.
ps class must come from a different class loader than
DelegatingPreparedStatement.class (the one explicitely written in your
source code).
So ps class and DelegatingPreparedStatement.class are not considered
the same class so instanceof returns false.

Simple tests:

System.out.println("ps.getClass().equals(DelegatingPreparedStatement.class)="+ps.getClass().equals(DelegatingPreparedStatement.class));
System.out.println("ps.getClass().getClassLoader().equals(DelegatingPreparedStatement.class.getClassLoader())="+ps.getClass().getClassLoader().equals(DelegatingPreparedStatement.class.getClassLoader()));

Both will return false.

You should ensure that all your web application external jars are
loaded by the same class loader.
One way would be to put all your jars under your web application
WEB-INF/lib folder.

Regards
Luís Amorim - 18 Sep 2006 14:17 GMT
Thank you for your replies, yogi and Jean.

Jean, I did the simple tests you suggested and both tests returned
false, indeed. So, those two classes come from different class loaders.

However, all external jars are already in WEB-INF/lib folder, so I
don't understand why both classes aren't loaded by the same class
loader.

Regards,
LA

> > ps instanceof DelegatingPreparedStatement is false
>
[quoted text clipped - 18 lines]
>
> Regards
Luís Amorim - 18 Sep 2006 16:12 GMT
Strange thing happened. I just deleted the jar that includes the
DelegatingPreparedStatement class from the WEB-INF/lib folder and now
the tests return true, which means both classes have been loaded by the
same class loader.

Oh well...

> Thank you for your replies, yogi and Jean.
>
[quoted text clipped - 30 lines]
> >
> > Regards
Andrea Desole - 19 Sep 2006 09:06 GMT
> Strange thing happened. I just deleted the jar that includes the
> DelegatingPreparedStatement class from the WEB-INF/lib folder and now
> the tests return true, which means both classes have been loaded by the
> same class loader.
>
> Oh well...

be careful. If it's still working it means that you are using the
Tomcat's jar. This can be good for you if you are using only Tomcat, but
your application might not work on other servers.
You should look better at how class loading in Tomcat works. There is
probably a way to tell Tomcat to load your jar file instead of the server's


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.