Doctrine "Closed Entity Manager"

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Doctrine "Closed Entity Manager"

Bas Kamer
Hello,


reading http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/transactions-and-concurrency.html#exception-handling

Ok Fine... With a closed entity manager I should get a new entity manager if i want to work continue doing queries...

What would be a good strategy to do that with the context of zf2 when the EM is injected via factories.

About the only thing I can think of is setting the shared flag of the entity manager to false… This doen’t seem efficient as that would (re)create connections not for the exception case but always.

Intend use is long running background processes - in which a closed entity manager really helpful


Bas
Reply | Threaded
Open this post in threaded view
|

Re: Doctrine "Closed Entity Manager"

Marco Pivetta
Either set the "shared" flag to `false`, or pass a factory down to your
service:

$myService = new MyService(function () use ($sm) { return
$sm->createService('Doctrine\ORM\EntityManager'); });

This saves you from having the SM bound to your service, and allows you to
use the callback as a "smart constructor"

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 21 January 2014 17:25, Bas Kamer <[hidden email]> wrote:

> Hello,
>
>
> reading
> http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/transactions-and-concurrency.html#exception-handling
>
> Ok Fine... With a closed entity manager I should get a new entity manager
> if i want to work continue doing queries...
>
> What would be a good strategy to do that with the context of zf2 when the
> EM is injected via factories.
>
> About the only thing I can think of is setting the shared flag of the
> entity manager to false… This doen’t seem efficient as that would
> (re)create connections not for the exception case but always.
>
> Intend use is long running background processes - in which a closed entity
> manager really helpful
>
>
> Bas
Reply | Threaded
Open this post in threaded view
|

Re: Doctrine "Closed Entity Manager"

seyfer
Marco Pivetta wrote
Either set the "shared" flag to `false`, or pass a factory down to your
service:

$myService = new MyService(function () use ($sm) { return
$sm->createService('Doctrine\ORM\EntityManager'); });

This saves you from having the SM bound to your service, and allows you to
use the callback as a "smart constructor"

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 21 January 2014 17:25, Bas Kamer <[hidden email]> wrote:

> Hello,
>
>
> reading
> http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/transactions-and-concurrency.html#exception-handling
>
> Ok Fine... With a closed entity manager I should get a new entity manager
> if i want to work continue doing queries...
>
> What would be a good strategy to do that with the context of zf2 when the
> EM is injected via factories.
>
> About the only thing I can think of is setting the shared flag of the
> entity manager to false… This doen’t seem efficient as that would
> (re)create connections not for the exception case but always.
>
> Intend use is long running background processes - in which a closed entity
> manager really helpful
>
>
> Bas
What if i'm not have access to ServiceManager in program part, where "EntityManager is Closed" error was occur?
Than i'm use this method

protected function checkEMConnection()
    {
        if (!$this->getEntityManager()->isOpen()) {
            $connection = $this->getEntityManager()->getConnection();
            $config     = $this->getEntityManager()->getConfiguration();

            $this->em = $this->getEntityManager()->create(
                    $connection, $config
            );
        }
    }