#[DbalTransaction] always fails with "Undefined array key" / TypeError
Ecotone version(s) affected: 1.314.0 to 1.328.2 (also present on 1.x at de38730b and on main)
Description
Any handler annotated with #[DbalTransaction] fails before it runs, even with the default configuration
(DbalConfiguration::createWithDefaults() and a single DbalConnectionFactory):
Warning: Undefined array key "Enqueue\Dbal\DbalConnectionFactory"
in src/DbalTransaction/DbalTransactionInterceptor.php:53
TypeError: DbalTransactionInterceptor::{closure}(): Argument #1 ($connectionFactory)
must be of type Interop\Queue\ConnectionFactory, null given
The global transaction flags (withTransactionOnCommandBus, withTransactionOnAsynchronousEndpoints) work.
Only the attribute path is broken.
How to reproduce
final class PersonService
{
#[DbalTransaction]
#[CommandHandler('person.register')]
public function register(int $personId, #[Reference(DbalConnectionFactory::class)] ConnectionFactory $connectionFactory): void
{
$connectionFactory->createContext()->getDbalConnection()->insert('persons', ['person_id' => $personId]);
}
}
$ecotone = EcotoneLite::bootstrapFlowTesting(
[PersonService::class],
[new PersonService(), DbalConnectionFactory::class => $connectionFactory],
ServiceConfiguration::createWithDefaults()
->withSkippedModulePackageNames(ModulePackageList::allPackagesExcept([ModulePackageList::DBAL_PACKAGE]))
->withExtensionObjects([DbalConfiguration::createWithDefaults()->withTransactionOnCommandBus(false)]),
);
$ecotone->sendCommandWithRoutingKey('person.register', 1); // throws the TypeError above
Possible Solution
DbalTransactionModule passes the connection factories to the interceptor as a list:
array_map(fn (string $id) => new Reference($id), $connectionFactories)
// [0 => Reference(DbalConnectionFactory::class)]
When the attribute is present, DbalTransactionInterceptor looks the factories up by the reference names
declared on the attribute ($this->connectionFactories[$connectionReferenceName]). That key does not exist in
the list, so the lookup returns null. Keying the factories by their reference names fixes it:
array_combine($connectionFactories, array_map(fn (string $id) => new Reference($id), $connectionFactories)),
I checked this locally on 1.x. A test with the attribute (commit on success, rollback when the handler
throws) fails before the change and passes after it. The Dbal package suite (277 tests) and PHPStan stay
green.
Context
None of the existing transaction tests use the attribute, which is probably why this went unnoticed. On
main, PulseSavepointIsolation is built from the same list. I have not checked whether it also looks
factories up by name.
Hey @pkusmierczyk,
I've merged your fix, you can test it on latest version :)