Question

We have what we believe is a fairly common XA use case:

  1. read a message from an in-queue
  2. write some data to a database
  3. write a response message to an out-queue (which is different from the in-queue)

However we also need a way to handle internal errors and poison messages. The control flow we have in mind is something like this:

  1. read the message from the in-queue
  2. write to the database
    • if there's an exception roll back the database transaction
    • if there's no exception run commit phase1 on the database
  3. if everything went fine (no rollback and commit phase1 ok) write a success message to the out-queue
  4. if either commit phase1 on the database failed or there was an exception and the database transaction was rolled back write a failure message to the out-queue
  5. commit the in-queue, the out-queue and the database (unless rolled by because of an exception)

Is this a good approach, should we do it differently? How can we do this with EJBs?

We're using EJB 3.1 on JBoss AS 7.2 / EAP 6.1, coding against Narayana directly is an option. The JDBC driver is ojdbc7-12.1.0.1 and the JMS RAR is MQ Series (don't know the version).

Was it helpful?

Solution

What you could do is to use the Java EE event mechanism to get a notification when your transaction fails and create a subsequent output message.

See

You need to use a new transaction to write to the out queue to avoid rolling back the message writing as well.

You will still have the message in the input queue which caused the exception, since the rollback will prevent the successfull consumption. You need to handle that separately, for example by the JMS provider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top