title: Symfony 3.4 - The EntityManager is closed
body: <p>I have an application run with Symfony 3.4 with MySql and I get a error: 'The EntityManager is closed'. My application running two ways:</p>
  
  <p>1 - A Console application thats is called by a sh script every time. This console app make many inserts in a database table.</p>
  
  <p>2 - A HTTP Route that also insert in same table. </p>
  
  <p>When de console app is running in background if i call the http route i get the error 'The EntityManager is closed'. If I stop de backgroud app the http route works. It's as if the two apps console and http use the same instance EntityManager.</p>
  
  <p>My code:</p>
  
  <p>I Create a service called AbstractRepositoryService. All of my services that manage repositories should extend.</p>
  
  <pre><code>&lt;?php
  
  abstract class AbstractRepositoryService
  {
  
      /**
       *  
       * @var EntityManagerIntergace - $em
      */
      protected $em;
  
      /**
       * 
       * @param EntityManagerInterface $em
      */
      public function __construct(EntityManagerInterface $em) {
  
          $this-&gt;em = $em;
      }
  
      /**
       *
       * 
       * @param String
       * 
       * @return @mixed
       * 
       * @throws RuntimeException
      */
      public function __call($method, $args) {
  
          $repository = $this-&gt;em-&gt;getRepository(static::ENTITY);
  
          if (!method_exists($repository, $method)) {
  
              throw new RuntimeException(
                  sprintf("Method '%s' not found.", $method), 
                  500
              );
          }
  
          try {
  
              return call_user_func_array(array($repository, $method), $args);
  
          } catch(Exception $e) {
  
              throw new Exception($e-&gt;getMessage(), 500);
          }
  
      }
  }
  </code></pre>
  
  <p>My UserRepositoryService where the exception is thrown in the <strong>flush method</strong></p>
  
  <pre><code>&lt;?php
  
  final class UserRepositoryService extends AbstractRepositoryService
  {
      /**
       * 
       * @const String
      */
      const ENTITY = 'AppBundle\\Entity\\User';
  
      /**
       * 
       * @param User
      */
      public function insert(User $user) {
  
          try {
  
              $this-&gt;em-&gt;persist($user);
              $this-&gt;em-&gt;flush($user);
  
          } catch (Exception $e) {
  
              throw new Exception($e-&gt;getMessage(), 500);
          }
      }
  }
  </code></pre>
  
  <p>And finaly my service declaration:</p>
  
  <pre><code>app.services.user_repository_service:
          public: true
          class: AppBundle\Services\UserRepositoryService
          arguments:
              - '@doctrine.orm.entity_manager'
  </code></pre>