Some changes to avoid cursors that are still open while session is being closed.