title: JMS doesn't rollback XA transaction (or doesn't participate in one)
body: <p>I'm relatively new to XA transactions. I've been struggling a few days to make a simple XA transaction work to no avail. </p>
  
  <p>First, I tried to use two different databases. I set up 2 XA datasources and had succeeded in rolling back the first database operation when the second fails. So far, so good. But then I tried to replace second datasource with JMS connectionFactory and cannot reproduce the same behavior.</p>
  
  <p>Here's the relevant code:</p>
  
  <p><strong>Database logic</strong>:</p>
  
  <pre><code>@Stateless
  public class FirstDB implements FirstDBLocal {
  
      @PersistenceContext(unitName = "xaunit")
      private EntityManager em;
  
      public void doSomething() {
          SomeEntity someEntity = em.find(SomeEntity.class, 1234L);
          someEntity.setSomeFlag(false);
      }
  
  }
  </code></pre>
  
  <p><strong>JMS code</strong>:</p>
  
  <pre><code>@Stateless
  public class SecondJMS implements SecondJMSLocal {
  
      @Resource(mappedName = "java:/JmsXA")
      private ConnectionFactory connFactory;
  
      @Resource(mappedName = "queue/Some.Queue")
      private Queue q;
  
      @Override
      @TransactionAttribute(TransactionAttributeType.MANDATORY)
      public void sendMsg() {
          Session session = null;
          Connection conn = null;
          MessageProducer producer = null;
          try {
              conn = connFactory.createConnection("guest", "guest");
  
              session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
  
              producer = session.createProducer(q);
  
              // Not sure if I need this, but I found it in the sample code
              conn.start();
  
              TextMessage tm = session.createTextMessage(new Date().toString());
              producer.send(tm);
  
              throw new RuntimeException("Fake exception");
          } catch (JMSException e) {
              e.printStackTrace();
          } catch (RuntimeException e) {
              e.printStackTrace();
          } finally {
              // close all resources
          }
      }
  
  }
  </code></pre>
  
  <p><strong>The glue code</strong>:</p>
  
  <pre><code>@Stateless
  public class TestDBandJMS implements TestDBandJMSLocal {
  
      @EJB
      private FirstDBLocal firstDBLocal;
  
      @EJB
      private SecondJMSLocal secondJMSLocal;
  
      public void doStuff() {
          firstDBLocal.doSomething();
          secondJMSLocal.sendMsg();
      }
  
  }
  </code></pre>
  
  <p><strong>XA Connection Factory configuration</strong> (everything is JBoss default, except for commented out security settings):</p>
  
  <pre><code>&lt;tx-connection-factory&gt;
        &lt;jndi-name&gt;JmsXA&lt;/jndi-name&gt;
        &lt;xa-transaction/&gt;
        &lt;rar-name&gt;jms-ra.rar&lt;/rar-name&gt;
        &lt;connection-definition&gt;org.jboss.resource.adapter.jms.JmsConnectionFactory&lt;/connection-definition&gt;
        &lt;config-property name="SessionDefaultType" type="java.lang.String"&gt;javax.jms.Topic&lt;/config-property&gt;
        &lt;config-property name="JmsProviderAdapterJNDI" type="java.lang.String"&gt;java:/DefaultJMSProvider&lt;/config-property&gt;
        &lt;max-pool-size&gt;20&lt;/max-pool-size&gt;
        &lt;!-- &lt;security-domain-and-application&gt;JmsXARealm&lt;/security-domain-and-application&gt; --&gt;
        &lt;depends&gt;jboss.messaging:service=ServerPeer&lt;/depends&gt;
     &lt;/tx-connection-factory&gt;
  </code></pre>
  
  <p>I also have very simple MDB which just prints out received message to console (not going to post the code, since it's trivial).</p>
  
  <p>The problem is, when the exception is thrown in JMS code, the message is still received by MDB and SomeEntity is successfully updated in the database code (whereas I expect it to rollback).</p>
  
  <p>Here is the <a href="http://pastebin.ca/2673551" rel="nofollow">JMS log</a>. One fishy thing that I see there is this:</p>
  
  <pre><code>received ONE_PHASE_COMMIT request
  </code></pre>
  
  <p>Like I said, I'm not too familiar with XA yet, but I expect to see here TWO_PHASE_COMMIT, because there should be 2 resources which participate in the active transaction.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It worked eventually, after I tried @djmorton's suggestion.
  One other important thing to keep in mind when working with JBoss 5.1 is that the lookup name for XA JMS <code>ConnectionFactory</code> is "java:/JmsXA". I tried the same with </p>
  
  <pre><code>@Resource(mappedName = "XAConnectionFactory")
  private ConnectionFactory connFactory;
  </code></pre>
  
  <p>and it didn't work.</p>