DBAL Support

Transactions, Asynchronous, Dead Letter Queue PHP DBAL

Installation

composer require ecotone/dbal

Module Powered By

Powered by powerful database abstraction layer Doctrine/Dbal and Enqueue for asynchronous communication

Configuration

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

Using Database Connection String

Symfony
Laravel
Symfony
# config/services.yaml
Enqueue\Dbal\DbalConnectionFactory:
class: Enqueue\Dbal\DbalConnectionFactory
arguments: ["pgsql://user:[email protected]: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:[email protected]: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.

Using Existing Connection

Symfony
Laravel
Symfony
# config/services.yaml
Enqueue\Dbal\DbalConnectionFactory:
factory: ['Ecotone\Dbal\DbalConnection', 'create']
arguments: ["@Doctrine\DBAL\Connection"]
Laravel
# Register Service in Provider
use Enqueue\Dbal\DbalConnectionFactory;
use Ecotone\Dbal\DbalConnection;
public function register()
{
$this->app->singleton(DbalConnectionFactory::class, function ($app) {
return new DbalConnection::create(app("Doctrine\DBAL\Connection"));
});
}

We register our DbalConnectionFactory under the class name Enqueue\Dbal\DbalConnectionFactory. This will help Ecotone resolve it automatically, without any additional configuration.

Using Manager Registry

If we want to make use of existing connection using Manager Registry, we can do it this way

Symfony
Symfony
# config/services.yaml
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 Message Channel, we need to create Service Context.

class MessagingConfiguration
{
#[ServiceContext]
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 Service Context to turn off this configuration. You may also add more connections to be handled.

class DbalConfiguration
{
#[ServiceContext]
public function registerTransactions() : array
{
return [
DbalConfiguration::createWithDefaults()
->withTransactionOnAsynchronousEndpoints(true)
->withTransactionOnCommandBus(true)
->withDefaultConnectionReferenceNames([
"Enqueue\Dbal\DbalConnectionFactory",
"AnotherDbalConnectionFactory"
])
];
}
}

Dead Letter

Enable Dead Letter

Using Service Context

#[ServiceContext]
public function dbalConfiguration()
{
return DbalConfiguration::createWithDefaults()
->withDeadLetter(true);
}

Dbal comes with full support for Dead Letter. You can read more about it here.

Setting up, Dbal Dead Letter as final error channel:

#[ServiceContext]
public function errorConfiguration() {
return ErrorHandlerConfiguration::createWithDeadLetterChannel(
"errorChannel",
RetryTemplateBuilder::exponentialBackoff(1000, 10)
->maxRetryAttempts(3),
"dbal_dead_letter"
);
}

In above scenario, message after failing 3 times, will be stored in database for future investigation.

Dead Letter Console Commands

Help

Symfony
Laravel
Symfony
bin/console ecotone:deadletter:help
Laravel
artisan ecotone:deadletter:help

Listing Error Messages

Symfony
Laravel
Lite
Symfony
bin/console ecotone:deadletter:list
Laravel
artisan ecotone:deadletter:list
Lite
$list = $messagingSystem->runConsoleCommand("ecotone:deadletter:list", []);

Show Details About Error Message

Symfony
Laravel
Lite
Symfony
bin/console ecotone:deadletter:show {messageId}
Laravel
artisan ecotone:deadletter:show {messageId}
Lite
$list = $messagingSystem->runConsoleCommand("ecotone:deadletter:show", ["messageId" => $messageId]);

Replay Error Message

Symfony
Laravel
Lite
Symfony
bin/console ecotone:deadletter:replay {messageId}
Laravel
artisan ecotone:deadletter:replay {messageId}
Lite
$list = $messagingSystem->runConsoleCommand("ecotone:deadletter:replay", ["messageId" => $messageId]);

Replay All Messages

Symfony
Laravel
Lite
Symfony
bin/console ecotone:deadletter:replayAll
Laravel
artisan ecotone:deadletter:replayAll
Lite
$list = $messagingSystem->runConsoleCommand("ecotone:deadletter:replayAll", []);

Delete Message

Symfony
Laravel
Lite
Symfony
bin/console ecotone:deadletter:delete {messageId}
Laravel
artisan ecotone:deadletter:delete {messageId}
Lite
$list = $messagingSystem->runConsoleCommand("ecotone:deadletter:delete", ["messageId" => $messageId]);