MODE-1861 Improved error handling and ensured result sets are always closed