Refactor connection initialization and cursor instantation for better Django 1.6+ behavior.
Guard against attempting to close and already closed cursor.