title: Spring / RabbitMQ : transaction management
body: <p>To simplify my problem, I have</p>
  <p><strong>App1 with @Transactionnal method createUser():</strong></p>
  <ul>
  <li>Insert new user in database</li>
  <li>Add async message in RabbitMQ so that the user receives a notification mail</li>
  <li>(potentially some additional code, but not much)</li>
  </ul>
  <p><strong>App2 with RabbitMQ message consumer</strong></p>
  <ul>
  <li>Consumes messages on the mailing queue in real-time</li>
  <li>Read mail data in database</li>
  <li>Send mail</li>
  </ul>
  <p>The problem is that sometimes, App2 tries to consume the RabbitMQ message before the transaction is even committed on App1. This means that App2 can't read the mail data on database because the user is not yet created.</p>
  <p><strong>Some solutions may be:</strong></p>
  <ul>
  <li>Use READ_UNCOMMITED isolation level on App2</li>
  <li>Add some delay in RabbitMQ messages delivery (or some RetryTemplate on the consumer)</li>
  <li>Change the way we send emails...</li>
  </ul>
  <p>I've seen there is a RabbitTransactionManager in Spring, but I can't understand how it is supposed to work. Internals of transaction handling stuff has always seemed to be a bit hard to understand and the documentation doesn't help so much either.</p>
  <hr />
  <p><strong>Is there a way to do something like this?</strong></p>
  <ul>
  <li>Add a message to a RabbitMQ queue in a @Transactionnal method</li>
  <li>When the transaction ends, the message is committed to the queue, and the changes are committed to the database</li>
  <li>So that the message can't be consumed before the DB transaction ends</li>
  </ul>
  <p>How? And what to expect for example if I send synchronous RabbitMQ messages instead of asynchronous messages? Would it block the thread waiting for a response or something?
  Because we do send sync and async messages for different usecases.</p>