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
factory: ['Ecotone\Dbal\DbalConnection', 'createForManagerRegistry']
4
arguments: ["@doctrine","default"]
5
6
Register Manager Registry under DbalConnectionFactory, if you want to make use of auto configuration.
7
Otherwise you will need to tell Message Channel, Transactions the name of Connection Factory.
Copied!

Doctrine ORM Support

If you have set up Manager Registry Connection, you may enable support for Doctrine ORM Repositories.
1
#[ServiceContext]
2
public function getDbalConfiguration(): DbalConfiguration
3
{
4
return DbalConfiguration::createWithDefaults()
5
->withDoctrineORMRepositories(true);
6
}
Copied!

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!

Document Store

DBAL provides support for Document Store, which is enabled by default. Every document is stored inside the "ecotone_document_store" table.

Standard Aggregate Repository

You may enable support for storing standard aggregates.
1
#[ServiceContext]
2
public function getDbalConfiguration(): DbalConfiguration
3
{
4
return DbalConfiguration::createWithDefaults()
5
->withDocumentStore(enableDocumentStoreAggregateRepository: true);
6
}
Copied!

In Memory Document Store

For testing purposes you may want to enable In Memory implementation.
1
#[ServiceContext]
2
public function configuration()
3
{
4
return DbalConfiguration::createWithDefaults()
5
->withDocumentStore(inMemoryDocumentStore: true);
6
}
Copied!

Table initialization

Table will be create for you, however this comes with extra SQL cost, to verify before adding new document, if table exists. After releasing you may want to disable the check, as you know, that the table already exists.
1
#[ServiceContext]
2
public function getDbalConfiguration(): DbalConfiguration
3
{
4
return DbalConfiguration::createWithDefaults()
5
->withDocumentStore(initializeDatabaseTable: false);
6
}
Copied!