composer require ecotone/dbal
In order to use Dbal Support
we need to add ConnectionFactory
to our Dependency Container.
# config/services.yamlEnqueue\Dbal\DbalConnectionFactory:class: Enqueue\Dbal\DbalConnectionFactoryarguments:- "pgsql://user:password@host:5432/db_name"
# Register Service in Provider​use Enqueue\Dbal\DbalConnectionFactory;​public function register(){$this->app->singleton(DbalConnectionFactory::class, function () {return new DbalConnectionFactory('pgsql://user:password@host:5432/db_name');});}
We register our DbalConnectionFactory
under the class name Enqueue\Dbal\DbalConnectionFactory
. This will help Ecotone resolve it automatically, without any additional configuration.
If we want to make use of existing connection using Manager Registry
, we can do it this way
# config/services.yaml# You need to have RabbitMQ instance running on your localhost, or change DSNEnqueue\Dbal\DbalConnectionFactory:class: Enqueue\Dbal\ManagerRegistryConnectionFactoryarguments:- "@doctrine"-connection_name: "default"
Register Manager Registry under DbalConnectionFactory
, if you want to make use of auto configuration.
Otherwise you will need to tell Message Channel, Transactions the name of Connection Factory
.
To create Dbal Backed Channel
, we need to create Application Context.
use Ecotone\Amqp\AmqpBackedMessageChannelBuilder;use Ecotone\Messaging\Annotation\ApplicationContext;use Ecotone\Messaging\Annotation\Extension;​/*** @ApplicationContext()*/class MessagingConfiguration{/*** @Extension()*/public function orderChannel(){return DbalBackedMessageChannelBuilder::create("orders");}}
Now orders
channel will be available in our Messaging System.
Ecotone Dbal
comes with support for transactions.
To enable transactions on specific endpoint, mark it with Ecotone\Dbal\DbalTransaction\DbalTransaction
annotation.
/*** @CommandHandler()* @DbalTransaction()*/public function sellProduct(SellProduct $command) : void{// do something with $command}
By default Ecotone
enables transactions for all Asynchronous Endpoints and Command Bus. You may use of ApplicationContext
to turn off this configuration. You may also add more connections to be handled.
use Ecotone\Dbal\Configuration\DbalConfiguration;use Ecotone\Messaging\Annotation\ApplicationContext;use Ecotone\Messaging\Annotation\Extension;​/*** @ApplicationContext()*/class ChannelConfiguration{/*** @Extension()*/public function registerTransactions() : array{return [DbalConfiguration::createWithDefaults()->withTransactionOnAsynchronousEndpoints(true)->withTransactionOnCommandBus(true)->withDefaultConnectionReferenceNames(["Enqueue\Dbal\DbalConnectionFactory","AnotherDbalConnectionFactory"])];}​}
Examples can be find here.