PHPackages                             mhujer/rabbit-mq-database-transaction-producer-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mhujer/rabbit-mq-database-transaction-producer-bundle

ActiveLibrary

mhujer/rabbit-mq-database-transaction-producer-bundle
=====================================================

Publish messages to RabbitMQ producer when database transaction was committed

2.1.1(4y ago)05.5kMITPHPPHP ~7.4 || ~8.0

Since Jul 3Pushed 4y agoCompare

[ Source](https://github.com/mhujer/Rabbit-Mq-Database-Transaction-Producer-Bundle)[ Packagist](https://packagist.org/packages/mhujer/rabbit-mq-database-transaction-producer-bundle)[ RSS](/packages/mhujer-rabbit-mq-database-transaction-producer-bundle/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (14)Versions (8)Used By (0)

RabbitMQ Database Transaction Producer Bundle
=============================================

[](#rabbitmq-database-transaction-producer-bundle)

**This package is a fork of [`vasek-purchart/rabbit-mq-database-transaction-producer-bundle`](https://github.com/VasekPurchart/Rabbit-Mq-Database-Transaction-Producer-Bundle) maintained for newer PHP and Symfony versions.**

---

**Publish messages to RabbitMQ producer when database transaction was committed**

> **Note:** This bundle expects you are using [Doctrine DBAL &amp; ORM Bundle](https://github.com/doctrine/DoctrineBundle) and [RabbitMqBundle](https://github.com/eMAGTechLabs/RabbitMqBundle)

### The problem

[](#the-problem)

[Transactions](https://en.wikipedia.org/wiki/Database_transaction) in databases ensure that in a series of operations either all of them are "completed" or none of them is. This is very important for most applications, because otherwise their state becomes broken. That is why most database systems provide transactions (at least at some level). New problem arises, when you are using multiple systems, because there is usually no way to ensure transactional behavior for operations spanning all of them.

This bundle provides solution to mitigate the most common situations which originate from this problem when using RabbitMQ with an SQL database (through Doctrine). Both SQL databases and RabbitMQ have their own transactions, but there is no way to extend the transactions between the systems, which can lead to many erroneous situations, typically:

1. You publish an ID to the RabbitMQ queue, which should be processed asynchronously, but it was never committed to the database.
2. You publish an ID to the RabbitMQ queue, which should be processed asynchronously, but it was *not yet* committed to the database.
3. Everything is committed to the database, but the accompanying message was never sent to the queue.

This is even more common if you are using nested transactions, because then it is especially difficult to tell just by looking at "local" code, when the transaction will actually be committed.

That is precisely the case when using Doctrine ORM, because even when you call `flush`, you cannot be sure, that there is no open transaction wrapping this call.

### What this bundle does

[](#what-this-bundle-does)

This bundle does not claim to "solve" the problem, because it's almost impossible, but it tries to mitigate most of the practical situations caused by the problem. When publishing messages to RabbitMQ, this bundle will check, if there is an open transaction (including nested) on the database connection and if not, it will send the message right away. But when it detects that there is an open transaction, then it will store the message and it will be sent only after and if all the transactions on the connection were committed.

When writing your code like in the example below, all the situations mentioned in the last section should not make problems:

```
