title: How to update and persist entity after the entity manager was closed due to an exception and then re-opend?
body: <p>I am working on a Symfony + Doctrine based project. When an exception occurs during a long running import operation (parsing data, creating entities, persisting them, etc.) the <strong>entity manager is closed</strong>.</p>
  <p>Also I tried to re-open the entity manager I cannot use it properly and I get <code>EntityNotFoundException</code> or <code>ORMInvalidArgumentException</code> when trying to persist other entities.</p>
  <p><strong>So: How to properly re-open a closed entity manager to be able to use it again?</strong></p>
  <hr>
  <p><strong>Details:</strong></p>
  <ul>
  <li>To avoid that other user actions conflict with a running import, the user account is locked before the import starts.</li>
  <li>This is done by querying an <code>ImportLock</code> entity for the current user from the DB. If no such lock exists a new entity is created. The lock is set to active and persisted. If an active lock already exists, another import is active and the current import is not started.</li>
  <li>A <code>transaction</code> is started to handle the import</li>
  <li>Import data is parsed, entities are created and persisted</li>
  <li>If everything is fine, the transaction is committed and the lock is set to inactive. Done.</li>
  <li>However, when data is imported which already exists, a <code>UniqueConstraintViolationException</code> is thrown which <strong>closes the entity manager</strong>.</li>
  <li>Although the exception is caught and the entity is reset, it is not possible to update the lock entity.</li>
  </ul>
  <p>Code:</p>
  <pre><code>$lockRepo = $this-&gt;entityManager-&gt;getRepository(ImportLock::class);
  $lock = $lockRepo-&gt;findOneByUser($user);
  
  if (!$lock) {
      $lock = new ImportLock();
  }
  
  if ($lock-&gt;isActive()) {
      // Import already running --&gt; cancel
      return;
  }
  
  $lock-&gt;setActive();
  $this-&gt;entityManager-&gt;persist($lock);
  $this-&gt;entityManager-&gt;flush();
  
  
  // Import
  try {
      $this-&gt;entityManager-&gt;getConnection()-&gt;beginTransaction();
      doImport();
      $this-&gt;entityManager-&gt;getConnection()-&gt;commit();
  } catch (\Exception $e) {
      $this-&gt;entityManager-&gt;getConnection()-&gt;rollback();
  
      if (!$this-&gt;entityManager-&gt;isOpen()) {
          // Step 1
          $this-&gt;doctrineRegistry-&gt;resetManager();
  
          // Step 2
          $lockRepo = $this-&gt;entityManager-&gt;getRepository(ImportLock::class);
          $lock = $lockRepo-&gt;findOneById($lock-&gt;getId()); // look up entity using Id to avoid using the $user which was handled by the &quot;old&quot; entity manager
      }
  } finally {
      $lock-&gt;setInactive();
      $this-&gt;entityManager-&gt;persist($lock);  // &lt;-- Exception
      $this-&gt;entityManager-&gt;flush();
  } 
  </code></pre>
  <p>Depending on what I try, I get different exceptions when trying to persist the lock:</p>
  <ul>
  <li><code>Doctrine\ORM\Exception\EntityManagerClosed</code> when the the entity manager is not re-opend / reset (Step1 and Step2 NOT performed)</li>
  <li><code>Doctrine\\ORM\\EntityNotFoundException(code: 0): Unable to find &quot;My\\Entity\\User\&quot; entity identifier associated with the UnitOfWork</code> when <code>$lock</code> is not reloaded after resetting the entity manager (Step2 NOT performed)</li>
  <li><code>Doctrine\\ORM\\ORMInvalidArgumentException(code: 0): A new entity was found through the relationship 'My\\Entity\\ImportLock#user' that was not configured to cascade persist operations for entity: My\\Entity\\User@621</code> when entity manger is reset and <code>$lock</code> is reloaded (both Step1 and Step2 performed).</li>
  </ul>
  <p>It seems that somehow the <code>$lock</code> which was queried/created using the &quot;old&quot; entity manager cannot properly be used with the &quot;new&quot; (re-opend) entity manager.</p>
  <p><strong>How to solve this?</strong></p>