The issue is similar to #284, but easier to catch in a more common setup, like so (Symfony 6.4):
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
...
replicas:
ro:
url: '%env(resolve:DATABASE_RO_URL)%'
In this case, you might end up with invisible writes if they're stuck within a transaction on the default connection, but you're doing a read with the readonly version (default.ro is its name with this config)
Indeed, this yields 2 different static connections, since the hash in
|
$key = sha1(json_encode($params)); |
will have different values for each.
eg:


This isn't surprising, to be fair, and I don't think the project can do much for it, but hopefully this saves someone some time.
The solution being to make sure your tests use a single connection per database (at least within contexts where you expect to see writes done by tests). Unfortunately Symfony doesn't make this easy.
In principle, you could add config/test/doctrine.yaml with:
doctrine:
dbal:
connections:
default:
use_savepoints: true
replicas: ~
But in practice, during configuration parsing, Symfony maps ~ to [] and then performs an array merge instead of replacement, and thus your default values with a replica remain active...
So you pretty much have to remove replicas from your default doctrine.yaml and configure it explicitly for all your environments that don't rely on this bundle.
I'm not entirely sure how this can be worked around, to be honest.
About the only way to work around it that I can see is to have the StaticDriver somehow force Doctrine to return a connection to the primary specifically, maybe checking for when $connection is instanceof \Doctrine\DBAL\Connections\PrimaryReadReplicaConnection and calling ensureConnectedToPrimary.
The issue is similar to #284, but easier to catch in a more common setup, like so (Symfony 6.4):
In this case, you might end up with invisible writes if they're stuck within a transaction on the
defaultconnection, but you're doing a read with the readonly version (default.rois its name with this config)Indeed, this yields 2 different static connections, since the hash in
doctrine-test-bundle/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php
Line 31 in f10de29
eg:
This isn't surprising, to be fair, and I don't think the project can do much for it, but hopefully this saves someone some time.
The solution being to make sure your tests use a single connection per database (at least within contexts where you expect to see writes done by tests). Unfortunately Symfony doesn't make this easy.
In principle, you could add
config/test/doctrine.yamlwith:But in practice, during configuration parsing, Symfony maps
~to[]and then performs an array merge instead of replacement, and thus your default values with a replica remain active...So you pretty much have to remove replicas from your default
doctrine.yamland configure it explicitly for all your environments that don't rely on this bundle.I'm not entirely sure how this can be worked around, to be honest.
About the only way to work around it that I can see is to have the StaticDriver somehow force Doctrine to return a connection to the primary specifically, maybe checking for when $connection is instanceof
\Doctrine\DBAL\Connections\PrimaryReadReplicaConnectionand callingensureConnectedToPrimary.