[Doctrine\\ORM\\ORMException]
The EntityManager is closed.
After I get a DBAL exception when inserting data, EntityManager closes and I\'m not
Same problem, solved with a simple code refactoring. The problem is sometime present when a required field is null, before do anithing, try to refactor your code. A better workflow can solve the problem.
For what it's worth I found this issue was happening in a batch import command because of a try/catch loop catching an SQL error (with em->flush()
) that I didn't do anything about. In my case it was because I was trying to insert a record with a non-nullable property left as null.
Typically this would cause a critical exception to happen and the command or controller to halt, but I was just logging this problem instead and carrying on. The SQL error had caused the entity manager to close.
Check your dev.log
file for any silly SQL errors like this as it could be your fault. :)
This is a very tricky problem since, at least for Symfony 2.0 and Doctrine 2.1, it is not possible in any way to reopen the EntityManager after it closes.
The only way I found to overcome this problem is to create your own DBAL Connection class, wrap the Doctrine one and provide exception handling (e.g. retrying several times before popping the exception out to the EntityManager). It is a bit hacky and I'm afraid it can cause some inconsistency in transactional environments (i.e. I'm not really sure of what happens if the failing query is in the middle of a transaction).
An example configuration to go for this way is:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
user: %database_user%
password: %database_password%
charset: %database_charset%
wrapper_class: Your\DBAL\ReopeningConnectionWrapper
The class should start more or less like this:
namespace Your\DBAL;
class ReopeningConnectionWrapper extends Doctrine\DBAL\Connection {
// ...
}
A very annoying thing is that you have to override each method of Connection providing your exception-handling wrapper. Using closures can ease some pain there.
I had this issue. This how I fixed it.
The connection seems to close while trying to flush or persist. Trying to reopen it is a bad choice because creates new issues. I tryed to understand why the connection was closed and found that I was doing too many modifications before the persist.
persist() earlier solved the issue.
// first need to reset current manager
$em->resetManager();
// and then get new
$em = $this->getContainer()->get("doctrine");
// or in this way, depending of your environment:
$em = $this->getDoctrine();
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.