The EntityManager is closed

前端 未结 17 1780
星月不相逢
星月不相逢 2020-11-29 18:36
[Doctrine\\ORM\\ORMException]   
The EntityManager is closed.  

After I get a DBAL exception when inserting data, EntityManager closes and I\'m not

17条回答
  •  没有蜡笔的小新
    2020-11-29 18:47

    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.

提交回复
热议问题