Multithreading Java

I have a very complex stand-alone Java program, which I call a “connector”, which uses multi-threading and the Notes Object Interface for Java.It is not an agent. It handles connections from clients, connects to a Domino server (creates a session), and returns data to those clients.

I am trying to understand exactly how my program should behave with multi-threading.

A. Not long ago, it was crashing.

B. Recently, it was throwing an exception.

C. Now, it appears to be behaving correctly; but, I’m not sure.

Every connection creates a unique session.

Theoretically, every connection executes on a separate thread.

Unfortunately, since the threads are managed from a fixed sized thread pool (which my code has no control over),

it is possible that multiple connections (sessions) will execute on the same thread.

A. Initially, here was how my “connector” program behaved:

  1. When the connection is created, my “connector” program:

a) calls NotesThread.sinitThread (if the Domino server is local)

b) creates a Domino session

  1. When the connection is closed, my “connector” program:

a) calls Session.recycle

b) calls NotesThread.stermThread (if the Domino server was local)

Unfortunately, I recently found out that there may not be a separate thread for every connection.

So, when I used to call NotesThread.stermThread when I was done with connection/session 1,

and then try to access something with connection/session 2 (on the same thread), the program would crash.

B. Then, I started tracking which threads had been initialized, and which threads and been terminated,

Before each Domino API call, I would make sure the thread had been initialized (and reinitialized if it had been terminated).

Even after that, sometimes my program would throw an exception since it was trying to use a view that another thread had recycled.

C. Someone suggested that the solution was to simply never call NotesThread.stermThread.

That seems to work for now - but I don’t believe it is the right long term solution.

I don’t know what NotesThread.stermThread does, and what happens if I don’t call it.

I don’t know if I now have a memory leak or other resource loss.

So, my bottom line questions are:

  1. What does NotesThread.stermThread do, and more importantly, what happens or might happen if I don’t call it?

    Please note, I am calling Session.recyle.

  2. What happens if I recyle a session or session object on a different thread than the one which created it?

Thanks,

Chuck

Subject: Multithreading Java

Since most people probably didn’t read all of my topic post to get to the important questions, I’ll repost just the questions:

So, my bottom line questions are:

  1. What does NotesThread.stermThread do, and more importantly, what happens or might happen if I don’t call it?

Please note, I am calling Session.recyle.

  1. What happens if I recyle a session or session object on a different thread than the one which created it?

Thanks,

Chuck

Subject: Multithreading Java

sinitThread and stermThread should be used once per thread.

If you have a thread pool, you will have to initialize the thread whenever it is started the first time, and terminate the thread in the finally clause of the run() method. There should be no resource issue, as most are part of the Session object (which is recycled).

Once a session is recycled, all child objects (including databases, views, …) are recycled as well so it is good practice to not pass these objects to different threads if the current thread may recycle the session (or the parent database of the view) at some time in the future. Most Notes objects are threadsafe (with the exception DbDirectory), so can be passed between different threads, but this makes it generally harder to spot recycle bugs.

cheers,

Bram

Subject: RE: Multithreading Java

I am only calling sinitThread once per thread.Unfortunately, I don’t currently have any control over the thread creation/destruction, or which thread my code is running over,

only the code is run on that thread.

I always make sure that sinitThread is called before I make any Domino API calls.

When I know we are done with the session, I recycle it.

At this point, I used to call stermThread also, but that caused other things to fail. So, I no longer am calling stermThread.

I want to know if that’s a problem (since the doc says I should be calling it).

The session and the other objects that are created from it may be used on different threads; but, it is only recycled once (but from which thread, I’m not sure).

Thanks,

Chuck