Flush database queries on DB thread shutdown

Don't discard the pending queries, but instead execute them (minus
SELECT queries) to make sure we don't lose user data.