2

Context: I have a microservice that at application BootStrap goes and gets all the classes it needs from another microservice as a Zip, then it loads all the classes this Zip contains and executes some code.

Problem:

What we are experiencing is that in some cases the service that gives the Zip with the classes does not answer (this is not the problem I want to address here).

The problem is that when this happens we throw a ClassNotFoundException and execute again and in this execution the programm again detects it needs to load a class it does not have... So it goes to the findClass() method and tries to get it by calling the Microservices asking for this class, but the service that gives the Zip with the class again does not respond so we throw another ClassNotFoundException... And again execute but this time it does not even try to call findClass() method, it is like Java is saying "okay this is definetely not here so im not even going to bother calling findClass()", I am trying to find where in the documentation is this specified (because I wanna see where is this specified, is it normal?).

Oracle Documentation ClassLoader

The closest I could find to what I experience is this

But in that case they do have the class, but the loader does not bother in loading it again because it detects it already has it, we are experiencing the same but in reverse, the loader does not have it, and it does not bother in trying again. Where is this in the docs?

BugsOverflow
  • 386
  • 3
  • 19

1 Answers1

4

Actually, the absence of a guaranty that multiple resolution attempts will be made is already enough to allow the behavior of remembering and reusing the result of the first symbol resolution attempt, whether it is a failure or success.

But it’s even mandated behavior, as specified in The Java Virtual Machine Specification, §5.4.3. Resolution:

Subsequent attempts to resolve the symbolic reference always fail with the same error that was thrown as a result of the initial resolution attempt.

This is not be confused with manual invocations of loadClass on a ClassLoader instance.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • At the end when you say not to be confused with manual invocation of loadClass, it also applies to manual invocation of findClass right? – BugsOverflow Dec 22 '22 at 13:33
  • 1
    No, class loader implementations usually only have a cache for loaded classes, so when calling `loadClass` manually, you may get different results when the first invocation fails but the second finds a class file. However, that’s depending on the particular `ClassLoader` subclass whereas the resolution of symbolic references is done by the JVM and leads to not calling into the class loader at all, when there’s a recorded failure. – Holger Dec 22 '22 at 14:36
  • Sorry for insisting but I really want to be able to understand it, so I think I get that manually calling the loadClass could get me a different result, so for example: lets suppose that it already failed in loading a particular class, and also keep in mind that internally at some point the loadClass method also calls the findClass method right? So lets suppose I invoke loadClass manually, it will ignore the previous failure for that particular class im trying to load and so it will call findClass to try and find the class I want to load, right? – BugsOverflow Dec 22 '22 at 14:52
  • 2
    Yes, `loadClass` will call `findClass`, even on a subsequent call, [online demonstration](https://tio.run/##tVK7coMwEOz9FTdUUIQfyHsyTpVH4XSZFLJ0tuUIiUgnPybjbyeSgRgMKXONhmV32VuxZht2sRafVVX6uZIcuGLOwTOTGr4nEKbBHTEKx8ZIAUV4m87ISr18/wBmly5ryAAPUf9kmEAbvOAaNG67YHqixpkbo5BpWEjr6E0WGBRkPV52OHevG7RWCuxgpTWEnFDU3le3N8FCi@NDEw00KzADWlmzdTXtxdCj8VpMdxxLkkb3ssSRizTKcvzyTLk0uU@ybEBqiL@ZxxlxumstgmFvr@4cU566GgQ9psrG1IcRDMOXYLZ3hEVuPOVl6IOUThPOlAqdMXDIjRZAIVoysD23tEjeanC@RJufah5EanWHFiS771XDVa7CX1DLY7ctsVVyRnyV/nFXuOsXPbJgoJxb/kOGyJzUksOkqn4A) – Holger Dec 22 '22 at 15:09