phpsymfonydoctrineproophdoctrine-dbal

Prooph Eventstore (PDO) and Doctrine DBAL results in multiple connections


Situation

I'm using Prooph for my commandbus, eventbus and eventstore within Symfony 4.3. Since not every aggregate needs to be eventsourced, we also use Doctrine DBAL to just simply CRUD those simple aggregates.

Within a given domain I have commands / handlers configured in my commandbus which use either eventsourced repositories OR DBAL repositories.

When injecting this commandbus into a CLI command this results in multiple db connections when running anything on the CLI.

Problem

When trying to delete/create the database (for pristine install or resetting the test environment) Postgres refuses because there is another active connection.

Could not drop database "api" for connection named default
An exception occurred while executing 'DROP DATABASE "api"':

SQLSTATE[55006]: Object in use: 7 ERROR:  database "api" is being accessed by other users
DETAIL:  There is 1 other session using the database.

So I can safely conclude that the problem rises when using services with a PDO connection in combination with services with a DBAL connection.

Solution (failed)

A solution I though of was to use the Doctrine DBAL $connection->getWrappedConnection() for the Prooph Eventstore. Obviously typehinting does not allow this (getWrappedConnection() returns a Connection-interface) but the actual Doctrine PDOConnection extends \PDO should it does work, I'm willing to accept the hackiness at this point! However, to no avail, still 2 connections.


Solution

  • In short, I used lazy loading to make sure no actual DB connections were made during kernel booting. Just be sure to composer require symfony/proxy-manager-bridge or your lazy: true will just be ignored silently (hence I thought it was no solution at first).