When db is closed, make sure write transactions are closed

The catch here is that because we use thread-local storage in
dbm_lmdb.c, we can only close the write transaction of the current
thread. Nevertheless this should only happen when the process exits
(signal handler or program finished) so only one thread is supposed to
exist.