Is db == null ... really null?

Got a servlet that uses a static variable. When I test for null it passes the test as not null, but when I refer to it within the same block of code I get a “NotesException: Object has been removed or recycled”… thereby saying the variable is actually null ?

The test is as simple as

if (adminDB == null){

// do some stuff in here.

} else {

System.out.println("Already Have Admin DB : "+ adminDB.getReplicaID());

}

The test passes adminDB as NOT null… so when the else executes it produces the above mentioned error… There are no recycles used anywhere in the servlet, and the variable is initially declared in the class as

private static Database adminDB = null;

Note that the class this variable resides in is not declared as static…

Subject: NotesSession thread Terminates independantly of the JVM thread.

The link to another discussion thread, (pardon the pun), below highlights almost the exact same problem. From my own experiementation I have come up with this theory. Think of 2 memory stacks… one for the JVM and one for the domino server.

It seems to be that even though I use static classes or variables, the exception that gets raised, “NotesException: Object has been removed or recycled”, is a notes exception which means that the JVM has passed it through to the domino server which has thrown a wobbly at my seemingly sensible request. The JVM is oblivious to the domino environment. Why ?

Probably because the database class like many other notes-java api’s are just wrappers, and so the JVM doesn’t really know what’s happening on the domino server. It only has an interpretation by these intermediary wrapper classes.

Once the notes session is finished with whatever it’s doing it just terminates it’s thread and everything belonging to it even though you have politely asked the servlet/JVM to keep static instances. A simple test for null, does not actually goto the domino server it only tests the wrapper. This weak linkage between these objects is probably for reasons of efficiency.

But the actual object on the domino server is looong gone.

So then your code running in the JVM thinks, “ok, it’s not null, now lets get that object”, by making a request to use a method/property. Now the JVM says to the domino server, “run adminDB.FOO()”. The domino server, for the trailer-park whore that it is sometimes, gets this request and says “Method FOO of what ? I gave that memory away ages ago, get stuffed… Object has been removed/recycled”. Thus a domino object that does not look null is in fact null, the wrapper is not null, but the actual object is no longer assigned.

So don’t expect requests for thread persistence to also remain persistent on the domino server. To solve this then, I re-instantiate the object every time the servlet is called. This ensures that the JVM object is synchronised with the domino session environment as well. I still use a static variable but it “freshened” in the servlet every time I run doGet/doPost.

If there is a clean way to handle threads and persistence in domino servlets I am all ears

http://www-10.lotus.com/ldd/nd6forum.nsf/0/66951de35ed7ce4885256ebe005e77a0?OpenDocument

I did find this from lekkimworld which is a first pass solution, but not quite suitable for my implementation.

http://lekkimworld.com/lekkimworld/replyToComment.action?entry=1137847652985&comment=1140529034976