H
H
HellWalk2021-10-04 10:57:41
symfony
HellWalk, 2021-10-04 10:57:41

How is EntityManager->clear() different from Doctrine->resetManager()?

There are several services on the symphony that listen to RabbitMQ (respectively, the services spin in an endless loop).

Of course, in such cases, you need to clear the EntityManager cache so that php does not crash with a memory limit.

In some services, this is done through EntityManager->clear(), in others, through Doctrine->resetManager()

At first glance, it seems that the same thing is done, but, in services with EntityManager->clear(), they still periodically fall with a limit memory.

Looked at the code of these methods:

public function clear($entityName = null)
    {
        if ($entityName !== null && ! is_string($entityName)) {
            throw ORMInvalidArgumentException::invalidEntityName($entityName);
        }

        if ($entityName !== null) {
            @trigger_error(
                'Calling ' . __METHOD__ . '() with any arguments to clear specific entities is deprecated and will not be supported in Doctrine ORM 3.0.',
                E_USER_DEPRECATED
            );
        }

        $this->unitOfWork->clear(
            $entityName === null
                ? null
                : $this->metadataFactory->getMetadataFor($entityName)->getName()
        );
    }


public function resetManager($name = null)
    {
        if ($name === null) {
            $name = $this->defaultManager;
        }

        if (! isset($this->managers[$name])) {
            throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
        }

        // force the creation of a new document manager
        // if the current one is closed
        $this->resetService($this->managers[$name]);

        return $this->getManager($name);
    }


At first glance, again, there is not much difference - clear() manually resets all parameters in UnitOfWork, resetManager() simply recreates the doctrine.

But there is a difference in practice. There are those who know the insides of the doctrine - what's the difference?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-10-04
@HellWalk

I remembered that the flow in the Doctrine can be due to the SQL Logger, which is in the Connection
When you do resetManager()- you kill this SQL Logger with its objects in memory
You need to do something like

$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

This can be done at the configuration level of the entire container or service, or in a command.
But try it in your console command first!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question