Skip to content

Commit f32c825

Browse files
bwaidelichmhsdesign
authored andcommitted
Re-use previously created event store instances
1 parent fcdbbf5 commit f32c825

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Neos.ContentRepositoryRegistry/Classes/Factory/EventStore/DoctrineEventStoreFactory.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,39 @@
77
use Doctrine\ORM\EntityManagerInterface;
88
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
99
use Neos\EventStore\DoctrineAdapter\DoctrineEventStore;
10-
use Neos\EventStore\EventStoreInterface;
1110
use Psr\Clock\ClockInterface;
1211

1312
class DoctrineEventStoreFactory implements EventStoreFactoryInterface
1413
{
14+
/**
15+
* @var array<string, DoctrineEventStore> Runtime cache for created event store instances to prevent too many connections
16+
*/
17+
private static array $instances = [];
18+
1519
public function __construct(
1620
private readonly EntityManagerInterface $entityManager,
1721
) {
1822
}
1923

2024
/** @param array<string, mixed> $options */
21-
public function build(ContentRepositoryId $contentRepositoryId, array $options, ClockInterface $clock): EventStoreInterface
25+
public function build(ContentRepositoryId $contentRepositoryId, array $options, ClockInterface $clock): DoctrineEventStore
2226
{
23-
// We create a new connection instance in order to avoid nested transactions
24-
$connection = DriverManager::getConnection($this->entityManager->getConnection()->getParams(), $this->entityManager->getConfiguration(), $this->entityManager->getEventManager());
25-
return new DoctrineEventStore(
26-
$connection,
27-
self::databaseTableName($contentRepositoryId),
28-
$clock
29-
);
27+
$dsn = $options['dsn'] ?? null;
28+
$hash = md5($contentRepositoryId->value . '|' . $clock::class . '|' . $dsn);
29+
if (!array_key_exists($hash, self::$instances)) {
30+
if ($dsn !== null) {
31+
$connection = DriverManager::getConnection(['url' => $dsn]);
32+
} else {
33+
// We create a new connection instance in order to avoid nested transactions
34+
$connection = DriverManager::getConnection($this->entityManager->getConnection()->getParams(), $this->entityManager->getConfiguration(), $this->entityManager->getEventManager());
35+
}
36+
self::$instances[$hash] = new DoctrineEventStore(
37+
$connection,
38+
self::databaseTableName($contentRepositoryId),
39+
$clock
40+
);
41+
}
42+
return self::$instances[$hash];
3043
}
3144

3245
public static function databaseTableName(ContentRepositoryId $contentRepositoryId): string

0 commit comments

Comments
 (0)