Skip to content

EntityManager/Connection not reset after rollback in batch loop (Symfony 5.4, Doctrine ORM) #2071

@nishant-tinturkar

Description

@nishant-tinturkar

When running a batch process in Symfony 5.4.48 using Doctrine ORM, I encounter persistent transaction errors after a rollback inside a loop. Even after calling $doctrine->resetManager('default'), the next iteration’s EntityManager/Connection remains in a "rollback only" state, causing Transaction commit failed because the transaction has been marked for rollback only.

Steps to Reproduce:

  1. Use Symfony 5.4.48 and Doctrine ORM (latest compatible version).
  2. In a service, inject ManagerRegistry and use $doctrine->getManager('default') and $em->getConnection() inside a loop.
  3. Begin a transaction for each iteration.
  4. On a business error, call $connection->rollBack() and $doctrine->resetManager('default').
  5. On the next iteration, call $doctrine->getManager('default') and $em->getConnection() again.
  6. Attempt to commit the transaction.

Expected Behavior: After rollback and resetManager, the next EntityManager/Connection should be fresh and allow new transactions/commits.

Actual Behavior: The next iteration’s EntityManager/Connection is still marked as "rollback only", and commit fails with:
Transaction commit failed because the transaction has been marked for rollback only.

Minimal Example:
<?php foreach ($items as $item) { $em = $doctrine->getManager(); $connection = $em->getConnection(); $connection->beginTransaction(); try { // ... business logic ... if ($shouldRollback) { $connection->rollBack(); $doctrine->resetManager(); continue; } $connection->commit(); } catch (\Throwable $e) { if ($connection->isTransactionActive()) { $connection->rollBack(); } $doctrine->resetManager(); continue; } }

Environment:
Symfony: 5.4.48
Doctrine ORM: 2.20.3
PHP: 7.4.30
OS: Windows

Additional context

  • Calling resetManager does not return a new EntityManager instance.
  • The issue persists even after reacquiring the EntityManager and Connection.

Questions

  • Is this the expected behavior for Doctrine ORM?
  • Is there a recommended way to fully reset the EntityManager/Connection after rollback in a batch loop?
  • Is this a bug or a limitation of the current Doctrine/Symfony integration?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions