DBAL Support

Installation

composer require ecotone/dbal

Configuration

In order to use Dbal Support we need to add ConnectionFactory to our Dependency Container.

Symfony
Laravel
Symfony
# config/services.yaml
Enqueue\Dbal\DbalConnectionFactory:
class: Enqueue\Dbal\DbalConnectionFactory
arguments:
- "pgsql://user:password@host:5432/db_name"
Laravel
# 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

Symfony
Symfony
# config/services.yaml
# You need to have RabbitMQ instance running on your localhost, or change DSN
Enqueue\Dbal\DbalConnectionFactory:
class: Enqueue\Dbal\ManagerRegistryConnectionFactory
arguments:
- "@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.

Message Channel

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.

Transactions

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 Ecotoneenables 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

Examples can be find here.