|
7 | 7 | use Doctrine\ORM\EntityManagerInterface;
|
8 | 8 | use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
|
9 | 9 | use Neos\EventStore\DoctrineAdapter\DoctrineEventStore;
|
10 |
| -use Neos\EventStore\EventStoreInterface; |
11 | 10 | use Psr\Clock\ClockInterface;
|
12 | 11 |
|
13 | 12 | class DoctrineEventStoreFactory implements EventStoreFactoryInterface
|
14 | 13 | {
|
| 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 | + |
15 | 19 | public function __construct(
|
16 | 20 | private readonly EntityManagerInterface $entityManager,
|
17 | 21 | ) {
|
18 | 22 | }
|
19 | 23 |
|
20 | 24 | /** @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 |
22 | 26 | {
|
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]; |
30 | 43 | }
|
31 | 44 |
|
32 | 45 | public static function databaseTableName(ContentRepositoryId $contentRepositoryId): string
|
|
0 commit comments