Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JMS sender (Stateless EJB) sends message even if transaction fails

 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

I've used the Container Managed Transaction in the JMS sender code (TestJMS.java) which sends message to a JMS Queue. The JMS message receiver is an MDB (TestJMSReceiver.java).
Problem: I want that if an exception is thrown inside the sender code TestJMS-->sendMessageTest1(String s) then the transaction should be rolled back which means:
(i) The Entity JmsTestTable should not be saved (look inside sendMessageTest1() method of TestJMS.java)
(ii) The JMS Message sent should be destroyed and it should not be received by JMS Receiver-MDB (TestJMSReceiver).
The (i) option happens but not the (ii) one. This means that message is still being sent to Queue and it is still received by the MDB which is clear from the logs generated by MDB.
The code listing follows:

Sender: TestJMS....method: sendMessageTest1(String s)




MDB: TestJMSReceiver



Best Regards,
Nitin
 
Mr sujeet khandelwal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can use user transaction to solve this issue.

@Resource
javax.transaction.UserTransaction userTx;



try {
userTx.begin();
conn= connectionFactory.createConnection();
session=conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//session=conn.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer=session.createProducer(destQueue);
ObjectMessage message=session.createObjectMessage();
message.setObject(s);
producer.send(message);
// Trying to persist an entity. It should not get saved if Exception occurs in this transaction.
JmsTestTable js=new JmsTestTable();
js.setMessage("[JMS Client]"+s);
jmsTestEao.makePersistent(js);
userTx.commit();
}
catch(Exception ex)
{
ex.printStackTrace();
userTx.rollback();
}
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try EJBContext instead SessionContext.

Maybe it will work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic