Fix handling of EOFException

Also, made rollback a bit more robust, so that it will reconnect if
there is an error. And extracted a couple helper methods to cleanup code