Use QThreadStorage of a per-thread DatabaseThreadData object which is destroyed when the thread finishes.