Fix resource leak issue of cursor

Unclose cursor exists.
I would prefer to add a finally block to the try/catch, and close the cursor there. This would involve moving the declaration of the Cursor object outside of the try/catch, but is a much clearer model. Same for the other file.

Also, please prepare the PR against the dev branch rather than master.