Add missing calls to db.close() that were causing a hang.