Thread![]() |
Description |
---|---|
The application thread | The thread that starts the transaction, does work within its scope and then terminates the transaction. |
The main thread | The thread that starts and stops the transaction service. |
The timer thread | The thread responsible for monitoring timeout of the transaction. This thread is started alongside the transaction and runs in the background. |
The two-phase commit thread(s) | Imported transactions are subject to two-phase commit termination by the remote initiator of the transaction. As per standard remoting practices this typically involves separate threads different from the application thread. In normal cases, the remote will start termination only after all application threads have finished. However, there are special cases that involve pending "orphans" for which the remote got a network timeout. Our code checks for this and rejects incoming two-phase commit requests when a local application thread is still active for the transaction. |
This will lead to an endless wait case, since each thread can only complete when it gets the lock its waiting for. But the locks they are waiting for are held by another waiting thread, so these locks are never freed.
There are some guidelines that, if enforced consistently, can avoid deadlocks.
In general, deadlocks will not happen (are impossible) if the following techniques are used throughout the code;:
Merely living up to the first rule will help, but is hardly practical in realistic applications. So the second rule is bound to be relevant as well. However, there is a problem…
The FSM observers (listeners) in the coordinator are problematic: the FSM is by definition a state holder, and its methods require synchronization. Also, the pre-enter mechanism (via listeners) was designed to prevent illegal state transitions, so pre-enter events are dispatched within the synchronized block(s) of code. Consequently, the first rule is violated, and we must absolutely make sure that the second rule holds at all times.
Let's look at this in more detail. The FSM callbacks (via the listeners) will call other classes unknown at design time, and within a synchronized block of code. This implies that the FSM object will be locked at the time when another object (the listener) is called. So we are in the (possible) situation where a locked object calls another object, possibly violating the second rule. How can we make sure that this does not give deadlocks? The answer is not so hard: according to the second rule, the order of locking must always be the same for all threads that hold locks in several objects. In the case of the FSM, this means that we should assume that the order of locking is always going to be of the form:We can't control whether or not other classes call back into the FSM. In fact, this is very likely to happen. However, we can avoid deadlocks if we respect the following rule at all times:
Because this applies to direct and indirect calls, we can restate this as follows:
To enforce this principle and make it clear in the code, please following this convention:
Some deadlocks have occurred in past releases, by violations of these basic rules:
If a process is blocked then view its thread dump via:
jstack pid
Where pid is the process ID. If you don't know the process ID (e.g., when running from Eclipse) then try this:
ps -ef | grep java