Fix wrong context used for error check of opening database