EntityManager 已关闭 [英] The EntityManager is closed

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

问题描述

[DoctrineORMORMException]   
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: YourDBALReopeningConnectionWrapper

这门课应该或多或少是这样开始的:

The class should start more or less like this:

namespace YourDBAL;

class ReopeningConnectionWrapper extends DoctrineDBALConnection {
  // ...
}

一件非常烦人的事情是,您必须覆盖提供异常处理包装器的 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天全站免登陆