avoid race condition when using HornetQ in Jboss

736 views Asked by At

I'm using Jboss 6.1 together with Hibernate and HornetQ. If I manipulate Data in my Database and add a message to the Queue that relies on the data changed just before I don't want that message to be processed before my data has been actually committed to the DB. So in order to avoid a race condition I'd like to get HornetQ into my Container Managed Transaction so the message would only be "committed" to the queue when the global Transaction is also committed.

Is this possible ? Any hints ?

1

There are 1 answers

3
Nicholas On

It is possible. You will need to use an XA transaction so you get a 2 phase commit so:

  • Use an XA DataSource for the connections to the database. (You are using an XA capable DB, right ?)
  • Use an XA HornetQ ConnectionFactory.

The easiest way, I have found, to validate that you are in fact running a 2PC transaction is:

  1. Place a debugger break line after both the JMS and JDBC activity is complete, but not committed
  2. Examine the transaction which should be an instance of com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple
  3. Examine the contents of the _resources field.

If the transaction is an XA 2PC, that field (which is a Hashtable) will contain 2 XAResources as keys, one for JMS and the other for JDBC.

I don't know which architecture you're most familiar with, but I would think that a container managed transactional stateless session EJB would be the easiest way to do this.