Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

If a db error occurs the entity manager is closed for all future messages #35360

Copy link
Copy link
Closed
@elzozo13

Description

@elzozo13
Issue body actions

Symfony version(s) affected: x.y.z

Description
This is kind of related with #29662. The entity manager is correctly cleared if everything goes fine. But for example if something goes wrong from mysql (like for example a constraint fails, or a duplicate key appears, etc.), the entity will remain closed.

This in turn results in failure of all messages that come after this one.

How to reproduce
This of course is not a real life case, but is the simplest way to reproduce the issue.

public function __invoke(SomeMessage $message) {
        $a = $this->em->getRepository(SomeEntity::class)->find(1);
        $this->em->clear();
        $this->em->persist($a);
        $this->em->flush();
}

Results in:

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '21187557' for key 'PRIMARY' ""","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
14:34:42 ERROR [messenger] Error thrown while handling message App\Message\OrderPort. Sending for retry #1 using 1000 ms delay. Error: "The EntityManager is closed." ["message" => App\Message\OrderPort^ { …},"class" => "App\Message\OrderPort","retryCount" => 1,"delay" => 1000,"error" => "The EntityManager is closed.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
14:34:42 ERROR [messenger] Error thrown while handling message App\Message\OrderPort. Sending for retry #1 using 1000 ms delay. Error: "The EntityManager is closed." ["message" => App\Message\OrderPort^ { …},"class" => "App\Message\OrderPort","retryCount" => 1,"delay" => 1000,"error" => "The EntityManager is closed.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
14:34:42 ERROR [messenger] Error thrown while handling message App\Message\OrderPort. Sending for retry #1 using 1000 ms delay. Error: "The EntityManager is closed." ["message" => App\Message\OrderPort^ { …},"class" => "App\Message\OrderPort","retryCount" => 1,"delay" => 1000,"error" => "The EntityManager is closed.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
14:34:42 ERROR [messenger] Error thrown while handling message App\Message\OrderPort. Sending for retry #1 using 1000 ms delay. Error: "The EntityManager is closed." ["message" => App\Message\OrderPort^ { …},"class" => "App\Message\OrderPort","retryCount" => 1,"delay" => 1000,"error" => "The EntityManager is closed.","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]

Possible Solution
My current solution to the problem is to wrap everything in a try catch block and kill the consumer like this (I could regenerate the manager or something like that but... I am restarting the consumer every couple of thousands of messages anyway so that was good enough for me at this point):

public function __invoke(SomeMessage $message) {
       try {
            $a = $this->em->getRepository(SomeEntity::class)->find(1);
            $this->em->clear();
            $this->em->persist($a);
            $this->em->flush();
       }
       catch (ORMException $exception) {
            // No codes no nothing... no comment
            if ($exception->getMessage() === 'The EntityManager is closed.') {
                dd('Closed because entity manager needed to be reset');
            }
            throw $exception;
       }
}

The problem with this is that I keep dragging that try catch block around for no other reason than catching that error (that is not specified as thrown either so the ide screams at me that is not thrown even though it is :) ).

I could ofc add an abstraction layer for that but I consider is the middleware business to deal with this (correct me if I'm wrong), when is clearing the entity as defined here #29662.

Additional context
I am using a rabbit + messenger environment.

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.