DBAL Support
Transactions, Asynchronous, Dead Letter Queue PHP DBAL

Installation

1
composer require ecotone/dbal
Copied!

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
1
# config/services.yaml
2
Enqueue\Dbal\DbalConnectionFactory:
3
class: Enqueue\Dbal\DbalConnectionFactory
4
arguments: ["pgsql://user:[email protected]:5432/db_name"]
Copied!
1
# Register Service in Provider
2
3
use Enqueue\Dbal\DbalConnectionFactory;
4
5
public function register()
6
{
7
$this->app->singleton(DbalConnectionFactory::class, function () {
8
return new DbalConnectionFactory('pgsql://user:[email protected]:5432/db_name');
9
});
10
}
Copied!
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
1
# config/services.yaml
2
Enqueue\Dbal\DbalConnectionFactory:
3
factory: ['Ecotone\Dbal\DbalConnection', 'create']
4
arguments: ["@Doctrine\DBAL\Connection"]
Copied!
1
# Register Service in Provider
2
3
use Enqueue\Dbal\DbalConnectionFactory;
4
use Ecotone\Dbal\DbalConnection;
5
6
public function register()
7
{
8
$this->app->singleton(DbalConnectionFactory::class, function ($app) {
9
return new DbalConnection::create(app("Doctrine\DBAL\Connection"));
10
});
11
}
Copied!
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
1
# config/services.yaml
2
Enqueue\Dbal\DbalConnectionFactory:
3
class: Enqueue\Dbal\ManagerRegistryConnectionFactory
4
arguments:
5
- "@doctrine"
6
-
7
connection_name: "default"
Copied!
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.
1
class MessagingConfiguration
2
{
3
#[ServiceContext]
4
public function orderChannel()
5
{
6
return DbalBackedMessageChannelBuilder::create("orders");
7
}
8
}
Copied!
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.
1
#[CommandHandler]
2
#[DbalTransaction]
3
public function sellProduct(SellProduct $command) : void
4
{
5
// do something with $command
6
}
Copied!
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.
1
class DbalConfiguration
2
{
3
#[ServiceContext]
4
public function registerTransactions() : array
5
{
6
return [
7
DbalConfiguration::createWithDefaults()
8
->withTransactionOnAsynchronousEndpoints(true)
9
->withTransactionOnCommandBus(true)
10
->withDefaultConnectionReferenceNames([
11
"Enqueue\Dbal\DbalConnectionFactory",
12
"AnotherDbalConnectionFactory"
13
])
14
];
15
}
16
17
}
Copied!

Dead Letter

Enable Dead Letter

1
#[ServiceContext]
2
public function dbalConfiguration()
3
{
4
return DbalConfiguration::createWithDefaults()
5
->withDeadLetter(true);
6
}
Copied!
Dbal comes with full support for Dead Letter. You can read more about it here.
Setting up, Dbal Dead Letter as final error channel:
1
#[ServiceContext]
2
public function errorConfiguration() {
3
return ErrorHandlerConfiguration::createWithDeadLetterChannel(
4
"errorChannel",
5
RetryTemplateBuilder::exponentialBackoff(1000, 10)
6
->maxRetryAttempts(3),
7
"dbal_dead_letter"
8
);
9
}
Copied!
In above scenario, message after failing 3 times, will be stored in database for future investigation.

Dead Letter Console Commands

Help

Symfony
Laravel
1
bin/console ecotone:deadletter:help
Copied!
1
artisan ecotone:deadletter:help
Copied!

Listing Error Messages

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

Show Details About Error Message

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

Replay Error Message

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

Replay All Messages

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

Delete Message

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

Last modified 6mo ago