instanceof strange behaviour

  • Thread starter =?iso-8859-1?B?THXtcyBBbW9yaW0=?=
  • Start date
?

=?iso-8859-1?B?THXtcyBBbW9yaW0=?=

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
 
Y

yogi

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...
 
J

Jean-Francois Briere

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
 
?

=?iso-8859-1?B?THXtcyBBbW9yaW0=?=

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

Jean-Francois Briere escreveu:
 
?

=?iso-8859-1?B?THXtcyBBbW9yaW0=?=

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...
 
A

Andrea Desole

Luís Amorim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top