Fixed bug on database close: the thread local instance is removed.