Core/DB: Fix crash on shutdown

Fix race condition crash on shutdown, closing MySQL connections with queries still being executed