private function end(int $flushCounter, int $totalCounter): void
    {
        if ($this->shouldRollback) {
            $this->rollback($flushCounter);
            $totalCounter = max(0, $totalCounter - $flushCounter);
        } else {
            $this->flush($flushCounter, false);
        }
        $this->eventDispatcher->dispatch(new EndProcessEvent($this, $totalCounter));
        $this->reset();
    }