• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try EJBContext instead SessionContext.

Maybe it will work.
 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic