Problems to watch out for
1. ClassLoader Collisions
Reloading classes in new ClassLoaders can result in a variety of
hard-to-debug problems. There are several things that your application
might do that will thwart the stragegy, including the following.
- Initially loading classes from a jar file, and then try
reloading them from the /WEB-INF/classes directory tree (that
generally won't work!).
- Storing instances of classes from your /WEB-INF/classes
directory in the application-scope or in other global tables.
- Storing instances of java.lang.Class or java.lang.ClassLoader in
any of your data structures.
- Saving instances of your classes in static variables, instance
variables of your Actions, or anywhere other than in the session
scope, request scope, or page scope.
- Anything else that allows classes loaded by a new ClassLoader to
encounter classes loaded by an older ClassLoader.
If this kind of problem does occur, the most likely observed failure is a
ClassCastException involving another class (from another ClassLoader) with
the same name as the expected class. If such an exception occurs only when
you reload classes, the reloading in combination with unintentionally
cached classes is the most likely cause.
This kind of problem can also occur in sessions that started before a
ClassLoader change. The typical problem is that objects in the Session
scope (such as the ActionForm) were loaded by a different ClassLoader
than the code that needs to use them (such as Action classes).
Consequently, the reloading functionality should only be used during
development, and old sessions should be abandoned when class changes
are made.
2. Hidden or Unintended Caching
The strategy for reloading classes is completely reliable, but unintended
file and/or data caching can interfere with reloading of configuration data.
In other words, when changes are detected and a new ClassLoader is put in
place, you will always observe the new classes. Under some circumstances
(such as when proxy caching is being used), modified configuration
information is not observed after the first change.
Most of the time this problem will be resolved properly on the second
modification, so you can fix this problem by modifying the configuration
file again and starting a second session.