users@glassfish.java.net

JMS transactionality troubles in session bean (x-post from forum)

From: David Wheeler <david_at_inomial.com>
Date: Mon, 18 Nov 2013 10:57:32 +1100

Hi,

I'm having troubles getting JMS to roll back when the enclosing transaction is rolled back. The messages are being consumed from the queue, even when the transaction is rolled back.

Here's my details
Glassfish 3.1.2.2
Session Bean, Bean Managed transactions

The theory of the class is that it processes messages from a queue in batches of 1000 (for performance reasons) but it should back out of the whole lot if an unhandled exception is encountered. However at the moment when it gets the "Bork" exception the transaction is rolled back, but all messages (10 of them) are acked and removed from message bus.

Is anyone able to tell me why? from my reading of the glassfish documentation this should be supported behaviour.

I've also tried using Container managed transactions with similar results.


  public void getMessages()
  {
    Session session = null;
    MessageConsumer consumer = null;
    
    boolean rollback = false;
    int processedThisBatch = 0;
    try
    {
      int commitAfter = 100;
      utx.begin();

      session = connection.createSession(true, 0);
      consumer = session.createConsumer(jmsQueue);
      
      rollback = true;
      while (processedThisBatch < commitAfter)
      {
        Message msg = consumer.receive(200);

        if (msg == null)
          break;

        // do stuff with msg
        log.info("Message " + msg.getJMSMessageID() + " processed");

        processedThisBatch++;
        if (processedThisBatch % 10 == 0)
          throw new Exception("Bork!");
      }

      consumer.close();
      session.close();
      utx.commit();
      log.info("Transaction committed");
      rollback = false;

    }
    catch (Exception e)
    {
      log.log(Level.SEVERE, "Exception caught processing test queue", e);
    }
    finally
    {
      try
      {
        if (rollback)
        {
          utx.rollback();
          log.info("Transaction rolled back");
        }
        session.close();
      }
      catch (Exception e2)
      {
        log.log(Level.SEVERE, "Failed to rollback tx", e2);
      }
    }

  }
_____________________________

David Wheeler - Lead Engineer david_at_inomial.com p +61 3 9663 3554
WINNER 2013 ACOMMS Awards for Services to Industry - Professional services
Telecommunications and Utility Billing, Rating, Policy and Session Management