EntityManager关闭 [英] The EntityManager is closed

查看:974
本文介绍了EntityManager关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Doctrine\ORM\ORMException]   
The EntityManager is closed.  

在插入数据后获得DBAL异常后,EntityManager关闭,我无法重新连接。

After I get a DBAL exception when inserting data, EntityManager closes and I'm not able to reconnect it.

我尝试过这样,但没有获得连接。

I tried like this but it didn't get a connection.

$this->em->close();
$this->set('doctrine.orm.entity_manager', null);
$this->set('doctrine.orm.default_entity_manager', null);
$this->get('doctrine')->resetEntityManager();
$this->em = $this->get('doctrine')->getEntityManager();

任何一个想法如何重新连接?

Anyone an idea how to reconnect?

推荐答案

这是一个非常棘手的问题,因为至少对于Symfony 2.0和Doctrine 2.1,在关闭后不可能重新打开EntityManager。

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.

我发现克服这个问题的唯一方法是创建自己的DBAL Connection类,包装Doctrine一个并提供异常处理(例如重试几次,然后将异常弹出到EntityManager)。这有点黑客,我恐怕会在事务环境中造成一些不一致(即我不太确定如果失败的查询在事务中间,会发生什么)。

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.

这篇关于EntityManager关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆