The EntityManager is closed The EntityManager is closed symfony symfony

The EntityManager is closed


My solution.

Before doing anything check:

if (!$this->entityManager->isOpen()) {    $this->entityManager = $this->entityManager->create(        $this->entityManager->getConnection(),        $this->entityManager->getConfiguration()    );}

All entities will be saved. But it is handy for particular class or some cases. If you have some services with injected entitymanager, it still be closed.


Symfony 2.0:

$em = $this->getDoctrine()->resetEntityManager();

Symfony 2.1+:

$em = $this->getDoctrine()->resetManager();


This is how I solved the Doctrine "The EntityManager is closed." issue.Basically each time there's an exception (i.e. duplicate key) or not providing data for a mandatory column will cause Doctrine to close the Entity Manager. If you still want to interact with the database you have to reset the Entity Manger by calling the resetManager() method as mentioned by JGrinon.

In my application I was running multiple RabbitMQ consumers that were all doing the same thing: checking if an entity was there in the database, if yes return it, if not create it and then return it.In the few milliseconds between checking if that entity already existed and creating it another consumer happened to do the same and created the missing entity making the other consumer incur in a duplicate key exception (race condition).

This led to a software design problem. Basically what I was trying to do was creating all the entities in one transaction. This may feel natural to most but was definitely conceptually wrong in my case. Consider the following problem: I had to store a football Match entity which had these dependencies.

  • a group (e.g. Group A, Group B...)
  • a round (e.g. Semi-finals...)
  • a venue (i.e. stadium where the match is taking place)
  • a match status (e.g. half time, full time)
  • the two teams playing the match
  • the match itself

Now, why the venue creation should be in the same transaction as the match? It could be that I've just received a new venue that it's not in my database so I have to create it first. But it could also be that that venue may host another match so another consumer will probably try to create it as well at the same time. So what I had to do was create all the dependencies first in separate transactions making sure I was resetting the entity manager in a duplicate key exception. I'd say that all the entities in there beside the match could be defined as "shared" because they could potentially be part of other transactions in other consumers. Something that is not "shared" in there is the match itself that won't likely be created by two consumers at the same time. So in the last transaction I expect to see just the match and the relation between the two teams and the match.

All of this also led to another issue. If you reset the Entity Manager, all the objects that you've retrieved before resetting are for Doctrine totally new. So Doctrine won't try to run an UPDATE on them but an INSERT! So make sure you create all your dependencies in logically correct transactions and then retrieve all your objects back from the database before setting them to the target entity. Consider the following code as an example:

$group = $this->createGroupIfDoesNotExist($groupData);$match->setGroup($group); // this is NOT OK!$venue = $this->createVenueIfDoesNotExist($venueData);$round = $this->createRoundIfDoesNotExist($roundData);/** * If the venue creation generates a duplicate key exception * we are forced to reset the entity manager in order to proceed * with the round creation and so we'll loose the group reference. * Meaning that Doctrine will try to persist the group as new even * if it's already there in the database. */

So this is how I think it should be done.

$group = $this->createGroupIfDoesNotExist($groupData); // first transaction, reset if duplicated$venue = $this->createVenueIfDoesNotExist($venueData); // second transaction, reset if duplicated$round = $this->createRoundIfDoesNotExist($roundData); // third transaction, reset if duplicated// we fetch all the entities back directly from the database$group = $this->getGroup($groupData);$venue = $this->getVenue($venueData);$round = $this->getGroup($roundData);// we finally set them now that no exceptions are going to happen$match->setGroup($group);$match->setVenue($venue);$match->setRound($round);// match and teams relation...$matchTeamHome = new MatchTeam();$matchTeamHome->setMatch($match);$matchTeamHome->setTeam($teamHome);$matchTeamAway = new MatchTeam();$matchTeamAway->setMatch($match);$matchTeamAway->setTeam($teamAway);$match->addMatchTeam($matchTeamHome);$match->addMatchTeam($matchTeamAway);// last transaction!$em->persist($match);$em->persist($matchTeamHome);$em->persist($matchTeamAway);$em->flush();

I hope it helps :)