Ensuring that cached db connection is only used in transactions

The mDb field should not be used outside transactions,
because the database may have been closed between transactions.

Also, fixing a race condition described in

Bug: 3106464

Change-Id: I002a107f3b8aafa1a58dfdab2980978b00fc5ec1
3 files changed