1

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 ?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
mostart
  • 105
  • 1
  • 2
  • 13

1 Answers1

2

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.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • Thanks for the answer. But I might be doing something wrong since it doesn't seem to work for me. I have two stateless session beans in jboss 6.1 One would send the message to HornetQ, the other would consume it. What would I have to annotate for the QueueConnectionFactory that will be injected ? (@Resource(mappedName = "java:/XAConnectionFactory" or @Resource(mappedName = "java:/JmsXA") ? And what should my jms-ds.xml contain ? Thanks for any further help. – mostart Jan 24 '12 at 06:46
  • Ah, so that's a bit different that what I originally thought. Do you want both SSBs to participate in the same transaction ? Normally, it would just be [JMS Sender & Database] or [JMS Receiver & Database] or both (with different transactions) but I am not really clear on what you're trying to achieve. Could you map it out in a textual diagram ? – Nicholas Jan 24 '12 at 20:18
  • 1
    Using a 2-phase commit in a messaging setting is in itself prone to race conditions. – László van den Hoek Sep 16 '13 at 13:40