Orm: When a RetryableException is thrown, entitymanager should not be closed

Created on 1 Feb 2017  路  3Comments  路  Source: doctrine/orm

When a RetryableException is thrown, entitymanager should not be closed

The "bad" code is here : https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/UnitOfWork.php#L400

RetryableException are DeadlockException and LockWaitTimeoutException

entitymanager should not be closed so we can retry the flush like that

/*
    * Since doctrine 2.6, you cannot retry a flush when an exception had appeared 
    * because the entity manager is closed and all entities detach, even with a RetryableException. 
    * This function won't work, bescause if there is an Exception, the em is closed. 

    private retryflush() {

        $retry = 0;
        while ($retry < 3) {
            try {
                $this->em->flush();
                return;
            } catch (RetryableException $e) {

                if ($retry >= 2) {
                    throw $e;
                }
                sleep(1);
                $this->reset();
                $retry++;
            } 
        }
    }
*/

New Feature

Most helpful comment

Is there a good approach to retrying after deadlocks in the meantime?

All 3 comments

Is there a good approach to retrying after deadlocks in the meantime?

Is there any "at least not so bad" and simple/quick solution before v3.x release?

For example, i have merge in loop and then make one flush with hundreds entities. But if there is any exception inside of loop 鈥撀燼 lot of changes inside loop are lost with log flooded by "EntityManager is closed". That's annoying.

Any solutions can be done here now except making flush/reset inside of loop (super bad idea) or level down to detach from orm and use raw dbal/sql queries?

Approaches were mentioned in #865, but it still will be an entity manager without persisted entities.

Was this page helpful?
0 / 5 - 0 ratings