Check to see if we can connect to the db before accessing it. Fixes #396.