2
votes

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.
  • I tried disabling all commands which have an eventsourced repository and the problem is fixed.
  • I tried disabling all commands which have a DBAL repository and the problem is fixed.
  • I tried not injecting this commandbus and the problem is fixed.

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.

2
I think problems comes from postgres itself. What happen if you run this commands : REVOKE CONNECT ON DATABASE TARGET_DB FROM public; Then: SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'TARGET_DB';JessGabriel
@JessGabriel I don't think so, it also happens when running in an entirely fresh environment (testing pipeline) where de containers + database is freshly created in docker... And as I said, when I remove either type of commands it also doesn't occur. So I'm fairly sure it's because Symfony's config component is initiating all services which causes two db connections.Christian Vermeulen

2 Answers

0
votes

The second argument on the constructor of the MySQLEventStore is the PDO connection, see https://github.com/prooph/pdo-event-store/blob/master/src/MySqlEventStore.php#L82. You can use the same PDO instance for Doctrine and EventStore, but it's kind of dangerous as you could interfere with its transaction handling. I would recommend closing the Doctrine connection in the tests before dropping the database instead.

0
votes

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).