-
-
Notifications
You must be signed in to change notification settings - Fork 469
Description
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:
- Use Symfony 5.4.48 and Doctrine ORM (latest compatible version).
- In a service, inject ManagerRegistry and use $doctrine->getManager('default') and $em->getConnection() inside a loop.
- Begin a transaction for each iteration.
- On a business error, call $connection->rollBack() and $doctrine->resetManager('default').
- On the next iteration, call $doctrine->getManager('default') and $em->getConnection() again.
- 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?