instanceof strange behaviour  
Author Message
Lu韘 Amorim





PostPosted: 2006-9-16 0:32:00 Top

java-programmer, instanceof strange behaviour 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





PostPosted: 2006-9-16 3:27:00 Top

java-programmer >> instanceof strange behaviour 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...



Lu韘 Amorim wrote:
> 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

 
Jean-Francois Briere





PostPosted: 2006-9-16 3:32:00 Top

java-programmer >> instanceof strange behaviour > 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韘 Amorim





PostPosted: 2006-9-18 21:18:00 Top

java-programmer >> instanceof strange behaviour 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:
> > 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韘 Amorim





PostPosted: 2006-9-18 23:12:00 Top

java-programmer >> instanceof strange behaviour 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...

Lu韘 Amorim wrote:
> 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:
> > > 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

 
 
Andrea Desole





PostPosted: 2006-9-19 16:06:00 Top

java-programmer >> instanceof strange behaviour Lu韘 Amorim wrote:
> 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