title: Replace the default entity manager with new one in Symfony
body: <p>I've a default entity manager which works good and manages the whole entities we have.</p>
  <p>But now, I would like to add another entity manager based on the same connection settings except the database name that changed (the credentials, tables etc. are the sames, but in another database).</p>
  <p>The config of the default connection:</p>
  <pre><code>doctrine:
      dbal:
          default_connection: default_conn
          connections:
              default_conn:
                  wrapper_class: Project\Dbal\Connection\ConnectionWrapper
                  url: '%env(DEFAULT_DB_URL)%'
  </code></pre>
  <p>The <code>ConnectionWrapper</code>:</p>
  <pre><code>class ConnectionWrapper extends Connection
  {
      public function __construct(array $params, Driver $driver, ?Configuration $config = null,
                                  ?EventManager $eventManager = null)
      {
          $params['dbname'] = DatabaseSingleton::getInstance();
  
          parent::__construct($params, $driver, $config, $eventManager);
      }
  
      public function switch(string $dbName): void
      {
          // close current connection if required
          if ($this-&gt;isConnected()) {
              $this-&gt;close();
          }
  
          // replace database url with new database name
          $params = $this-&gt;getParams();
          $url = substr($params['url'], 0, strlen($params['url']) - strlen($params['dbname'])) . $dbName;
  
          // replace connection's params
          $params['url'] = $url;
          $params['dbname'] = $dbName; // new_database_name
          DatabaseSingleton::setValue($dbName);
  
          // initialize again the connection with updated connection's parameters
          $this-&gt;__construct($params, $this-&gt;_driver, $this-&gt;_config, $this-&gt;_eventManager);
      }
  }
  
  </code></pre>
  <p>The code in the controller that carries the change of the database name (that's to say, the connection (and em) should be changed after calling this controller action):</p>
  <pre><code>/**
   * @Route(&quot;/e2e&quot;, methods={&quot;PATCH&quot;})
   */
  public function switchDatabase(Request $request): void
  {
     // switch connection's database name
     /** @var ConnectionWrapper $conn */
     $conn = $this-&gt;getDoctrine()-&gt;getConnection();
     $conn-&gt;switch('new_database_name', $logger);
  
     // creating new entity manager
     /** @var EntityManagerInterface $em */
     $em = $this-&gt;getDoctrine()-&gt;getManager();
     $this-&gt;getDoctrine()-&gt;resetManager();
     $newEm = EntityManager::create($conn, $em-&gt;getConfiguration(), $em-&gt;getEventManager());
  
     $user = new User('chris');
     $newEm-&gt;persist($user);
     $newEm-&gt;flush();
  
     // how to replace the current (old) entity manager with the new one?
     // how to retain this new for all other futures requests?
  }
  </code></pre>
  <p>The problem is in other controller, when I want to retrieve my users. It's the old entity manager (with the old database name) that's used instead of my new manager..</p>
  <pre><code>public function test(EntityManagerInterface $em): JsonResponse
      {
          
          $entities = $em-&gt;getRepository(User::class)
                         -&gt;findBy(['username' =&gt; 'chris']);
  
          return new JsonResponse($user); // no result.. !
      }
  </code></pre>
  <p>How to replace the default entity_manager with the new created one on all the Symfony application (and not just inside my controller with <code>$newEm</code> variable).</p>
  <p>The problem is I cant use my <code>$newEm</code> for queries in my controller. But I don't succeed to replace the default entity manager of my Symfony application with this new manager.</p>
  <p>PS: my singleton class seems to be not worker...</p>