You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Robert A. Lentz" <ra...@ralentz.com> on 2000/11/25 03:07:57 UTC

Re: ClassCastException

Greetings,

I have just struggled with the dreaded ClassCastException and servlet 
re-loading... after pulling my hair out for a little while I made my way to 
the mailing list archives and found that this is currently expected behavior.

Disclaimer: I know absolutely nothing about class loaders...

given that, I will mention that one thing I tried was to create a dummy 
class member of the type that was the cast that was causing the exception, 
with the idea of ensuring that the class was already loaded.  Not even this 
worked.  Is this just functionality that has not yet been engineered or the 
way that class loaders are supposed to work?

Perhaps this exception can be caught at some point and a custom exception 
created which includes a mention in the message to the appropriate FAQ 
entry? :-)

(By the way, it seems to me that several Jakarta projects (Tomcat and ant 
are two of which I am aware) are implementing class loaders, perhaps 
resources can be pooled?)

I'm not yet on the mailing list due to traffic, please ensure I receive a 
copy of any response.

Thank you,
-Robert

-- 
ralentz@erols.com
  "It has been said that man is a rational animal. All my life
   I have been searching for evidence which could support this."
                                            -- Bertrand Russell


Re: ClassCastException

Posted by Kurt Bernhard Pruenner <le...@gmx.at>.
"Robert A. Lentz" wrote:
> I have just struggled with the dreaded ClassCastException and servlet
> re-loading... after pulling my hair out for a little while I made my way to
> the mailing list archives and found that this is currently expected 
> behavior.
> 
> Disclaimer: I know absolutely nothing about class loaders...
> 
> given that, I will mention that one thing I tried was to create a dummy
> class member of the type that was the cast that was causing the exception,
> with the idea of ensuring that the class was already loaded.  Not even this
> worked.  Is this just functionality that has not yet been engineered or the
> way that class loaders are supposed to work?

Well, having looked at the source for the AdaptiveClassLoader used in Tomcat,
I think I can tell you what happens.

If you're using classes that are loaded via the AdaptiveClassLoader (i.e.
classes not found in the system class path, but in WEB-INF/classes or
WEB-INF/lib), they're "abandoned" along with the current AdaptiveClassLoader
for your servlet's context; i.e. the reference to the class loader is thrown
out which makes it garbage collectible (if there weren't some classes like
those in your session that were still referencing it, but that's not the point
here). 

Then a new AdaptiveClassLoader is instanciated and all the classes are loaded
again on first use using the new ClassLoader, while the objects that are still
in your session (which isn't abandoned when reloaded, as the Session class was
loaded via the system class loader) were loaded using the old ClassLoader -
and that's where the trouble starts, as objects from the old ClassLoader are
really of a different kind as those from the new one; basically, you've now
got 2 different instances of the same class (not objects!) that are mutually
incompatible, and thus you get a ClassCastException when you try using the old
objects with the new ClassLoader...

(Of course, if you would only be using things like Strings or HashTables or
whatever that get loaded via the system class loader, you'd have no problem.)

I guess a solution would be to invalidate the session when such a
ClassCastException occurs, but of course the exception is likely to happen in
your own code and not at a point where Tomcat can do this for you; maybe an
option per context for "invalidating all sessions on reload" (which shouldn't
be too tragic in a development environment, which servlet reloading is really
meant for) would be helpful...

Phew... I hope I wasn't too far off with my attempt at an explanation... :)

-- 
Kurt Bernhard Pruenner --- Haendelstrasse 17 --- 4020 Linz --- Austria
Music: http://www.mp3.com/Leak --- Work: http://www.ssw.uni-linz.ac.at
.......It might be written "Mindfuck", but it's spelt "L-A-I-N".......
np: Vladislav Delay - Huone (Multila)