Yet another "already loaded in another classloader" problem

I have a strange dll (JNI) issue, and it goes like this:

There are two web query open agents on the server, both using the same DLL. Now, I know that when two agents run at the same time, the server throws an error.

But there is one major difference between them.

One of them works almost always, i.e. if it crashes, you just wait a sec, hit reload, and it will work. It is as expected, it can not load the dll twice, so all you need to do is wait for the dll to unload, and it works.

But the other agent almost NEVER works. It uses the same dll and the same java library, but it crashes almost every time. If I try the agent for the first time it works, but then it throws the error even if I wait 1 second, 5 seconds, 30, seconds, 1 minute, it doesn’t matter. This other agent needs and awfully long time to release the library, and for me, there is no explanation.

For a moment there I though it was explicit recycling, but that didn’t help.

Can someone please help?

Miha Vitorovic

Subject: Yet another “already loaded in another classloader” problem

Why don’t you delegate DLL access to a single thread safe class (ie: serializing the calls to the DLL).

Seems like the perfect candidate to me.

cheers,

Bram

Subject: RE: Yet another “already loaded in another classloader” problem

Well, I’m a bit confused by this whole Domino JVM business anyway.

I mean, what exactly is shared and what isn’t, when it comes to web agents? Does each one of them have its own unique context or not? Can they communicate with one another, or not? Can they share memory and static variables?

My knowledge of the inner workings of the JVM isn’t too deep? I wouldn’t be using DLLs anyway if Java had half-decent Time Zone conversion to begin with.

And now I’m having problems with JNI as well. AFAIK, if this was one single environment, statically loading a library shouldn’t present a problem anyway - the JVM would just load it once, but it looks like each web agent has a separate environment, until you try to load a library, that is. Then it suddenly discovers that it’s already loaded and throws an Exception, that can’t be caught. Unloading a library is impossible.

I’m just whining anyway… I’ll try to decipher what exactly you’re suggesting :), and see if it helps. My only problems is, that I don’t have time to learn everything about the inner workings of Domino server as well. The Notes API should hide that from the developer, not force him to discover exactly how the JVM initalization and classloaders work - without good documentation that is.

At least the documentation could be more extensive when it comes to that area. Anybody knows any good books in that area? Windows specific?

Cheers, Miha Vitorovic

Subject: RE: Yet another “already loaded in another classloader” problem

AFAIK the following is happening:

  1. On startup, the domino server loads a couple JVMs

→ One for the AMGR for scheduled/background agents

→ One for the HTTP task for web agents and servlets

  1. For Each of these JVMs the “system classloader” has access to the system/notes classes and the classes specified in JavaUserClasses in notes.ini

  2. For each execution of an agent, a new ClassLoader instance is created, that does the following when loading a class:

→ If it exists in the agent: load directly

→ If it exists in a library configured by the agent: load directly

→ If it is an imported java class in the agent document: load directly

→ Otherwise delegate to the system classloader (see number 2).

Your problem is simply that your class is either included in the agent or a referenced library. Hence it will get loaded by the agent classloader, for which a new instance is created on every agent execution.

The solution is simple: make sure your class is not in the agent, but available on the server as a “JavaUserClass” so it will get loaded by the system classloader, for which there is only one instance per JVM.

Keep in mind that using this architecture, multiple agents can have access to your class at once so it MUST be thread safe.

cheers,

Bram

Subject: RE: Yet another “already loaded in another classloader” problem

Thanks, I feel we’re on to something.

I have detached the JAR file to the server file system, and set the ‘JavaUserClasses’ variable, and set the same thing on my Client.

I can now get my Library to compile, but the agent now crashes with the following exception:

java.lang.NoClassDefFoundError: si/nil/timzoneinfo/TimeZoneInformation

at Util.getTimeZone(Util.java:364)

at Util.dConvertToTimeZone(Util.java:236)

at JavaAgent.NotesMain(JavaAgent.java:86)

at lotus.domino.AgentBase.runNotes(Unknown Source)

at lotus.domino.NotesThread.run(NotesThread.java:218)

The si/nil/timzoneinfo/TimeZoneInformation class is in the specified JAR file, is specified as ‘public’. The server has a lock on the JAr file, so obviously it is loaded, only the agent can not get to it.

Cheers, Miha Vitorovic

Subject: RE: Yet another “already loaded in another classloader” problem

Have you restarted the server?

(Whenever there is an update to the libraries in JavaUserClasses, a server restart is required for the system classloader to actually reload the classes).

If it is in the jar file, the server should always find it.

You should also make sure that all classes in a particular package are either in the JavaUserClasses jar OR in the agent and/or libraries. IIRC, the agent classloader has some funky reasoning that if a package is available in the agent, it won’t try to look up classes in that package via the system classloader (or vice versa, I don’t remember right now).

So just make sure that any classes that you’ll be storing on the server are in a different package than the ones you’ll be storing in your agent and/or script libraries.

cheers,

Bram

Subject: RE: Yet another “already loaded in another classloader” problem

Woohoo!!

It started working, finally! And it doesn’t crash either. I needed to restart the server one more time.

Thank you, thank you, thank you… Bow, praise, bow…

I seem to know something more about how Domino server works. Did you deduce that on a trial and error basis, or is the whole thing described somewhere in a book?

Does anyone know of a good book, that covers what developers need to know if they need something more than basic Domino funcionality?

Cheers, Miha Vitorovic

Subject: RE: Yet another “already loaded in another classloader” problem

I’ve figured this out with the tried and true “trial and error”, because I needed to write some custom classloaders for Domino R5 and R6 (don’t ask).

It is not documented in detail in the Notes Designer documentation, but it may well be that Julie Kadashevich has documented this somewhere in her (excellent) Agent Faq or Troubleshooting Agents in R5/R6. See also http://www-10.lotus.com/ldd/46dom.nsf/0/574c99ccb345839185256976004e811e?OpenDocument

Anyhow, knowledge about the inner workings of the classloader on Domino is not really a must for 99% of all cases out there.

There may be books on this topic (I remember that there was a book specifically on java development for Lotus Notes), but I’m not sure if they cover this particular topic.

Glad that it works.

cheers,

Bram

Subject: RE: Yet another “already loaded in another classloader” problem

Well, I find that more and more often the things that are bothering me fall within that 1%.

Thanks again…

Subject: RE: Yet another “already loaded in another classloader” problem

It was a little late yesterday :(, but anyway, the problems I am having is not with calling the native function, it is somehing I have no control over.

Before I get to call my functions, the DLL gets loaded:

public class TimeZone {

static {

	System.loadLibrary( "TimeZoneLib" );

}

…

This is where the JVM throws an exception. So, the JVM should detect that DLL is already loaded and not load it again. Or, if one could catch that exception [UnsatisfiedLinkError] and ignore it [because it simply means that the DLL is already loaded], everything would be alright. But that Exception, or rather error can not be caught, it is simply reported before the program exits.

I have tried to load the DLL as needed, but I have found no way to detect, that a DLL is already loaded. All you can do is try to load it again, because loading it statically should only load it once anyway.

Now, as I have stated, I don’t know that much about JVM, so maybe my understanding is flawed, but searching the Internet indicates, that it is a known bug in the JVM, and that it cannot be fixed.

But, even if it cannot be fixed, I still want to understand why two of my agents behave so differently.

One of them crashes only if two versions of it run concurrently [two users open tha same page at the same time], or one briefly after another.

The second agent works only once every few minutes, and crashes predictivly in between, like something is not releasing the loaded library, even though the agent has stopped running long ago.

Both are using the same DLL, and the same library, so the DLL loading part is the same for both of them-

And I want to know what that is, and how to fix it. Maybe it is a the size of the agents? The first one larger, and maybe the server unloads it ASAP, so that it will not waste its resources.

The other one is short and simple, so maybe it doesn’t get unloaded for a while.

But, if that is the case, how do I fix it, besides trying to eat up large ammounts of memory :((((((

Cheers, Miha Vitorovic

Subject: RE: Yet another “already loaded in another classloader” problem

An easier solution to this problem is simply to place the jar file in a server or client directory and have notes client or server ini file (javaclasspath) i.e. c:\windows\java\classes\yourjarfile.jar specifically point to that jar file. That way the dlls are loaded once by the JVM and not each time the agent is triggered on each notes thread. If the jar file is embedded in the Notes java agent take it out.

Lee

http://www.noteshound.com