2 Replies Latest reply on Aug 3, 2011 5:42 AM by Optimus Prime

    Transaction question

    Optimus Prime Newbie

      Hi all, I'm starting with JMS and I'm doing some samples before going mainstream.

       

      I'm trying to get the whole transaction + jms to work, but I think I might misunderstood the concept, please correct me :

       

      From what I understood, when using a session inside a JTA (XA) transaction, in case of message producing, the message will only be sent to the destination after the underlying TX is commited. And when consuming the message will only be acknowledge after the tx is commited as well. This would give the chance of other resources (EJBs, PersistenceContexts) to be part of the transaction right?

       

      Well I've tried this but it;s not working, my scenario:

       

      A REST (CDI) endpoint creates a XASession and sends a message, inside the same method it calls a SLSB that persists an entity (this step fails on purpose). I was expecting the message to never be sent, since the SLSB rolledback the TX, but it's sent just after the call to producer.send...

       

      What am I missing here?

       

      Here's my small code:

       

       

      public class MessageSenderImpl implements MessageSender {
      
      
                @Resource(mappedName="queue/test") 
                private Queue destination;
        
                 @Resource(mappedName="/XAConnectionFactory")
                private XAConnectionFactory xa;
        
                @Inject
                private TransactionalOrderService txService;
      
      
      @Override
                @TransactionAttribute(TransactionAttributeType.REQUIRED)
                public void sendTx(String message) {
                          XAConnection conn = null;
                          XASession session = null;
                          try {
                                    conn = xa.createXAConnection();
                                    session = conn.createXASession();
                                    MessageProducer producer = session.createProducer(destination);
                                    TextMessage txtMessage = session.createTextMessage(message);
                                    producer.send(txtMessage);
                                    Order order = new Order();
                                    order.setId(1);
                                    txService.save(order);
                          } catch (Exception e) {
                                    e.printStackTrace();
                          }finally{
                                    try {
                                              conn.close();
                                    } catch (JMSException e) {
                                              e.printStackTrace();
                                    }
                          }
                }
      }
      
      @Local
      public interface TransactionalOrderService {
      
      
                @TransactionAttribute(TransactionAttributeType.REQUIRED)
                public void save(Order order);
      }
      
      public @Stateless class TransactionalOrderServiceBean implements TransactionalOrderService {
      
      
                @PersistenceContext(name="txUnit")
                private EntityManager em;
        
                @Override
                public void save(Order order) {
                          em.persist(order);
        
                }
      
      
      }
      
      
      

       

       

      Have I got the whole TX concept wrong?

       

      Any help/advice is MOST appreciated

       

      Best Regards