BaseXdb/basex — pull request #992 by dirkk (2014-09-22 08:04:11+00:00)
Title: [MOD] Do not expose server object before construction finished.

As discussed, I do think exposing the object before construction is complete is always a problem.

From www.ibm.com/developerworks/java/library/j-jtp0618/index.html:

> The practices detailed above for thread-safe construction take on even more importance when we consider the effects of synchronization. For example, when thread A starts thread B, the Java Language Specification (JLS) guarantees that all variables that were visible to thread A when it starts thread B are visible to thread B, which is effectively like having an implicit synchronization in Thread.start(). If we start a thread from within a constructor, the object under construction is not completely constructed, and so we lose these visibility guarantees.

This is just a first commit, as I wanted to discuss if you agree with the problem.

To fix this issue in the complete code base requires a bit more work, because there are quite a number of occurences of code in constructors like

Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public synchronized void run() {
context.close();
}
});

which is also unsafe (in my opinion).