jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes JMS sender (Stateless EJB) sends message even if transaction fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JMS sender (Stateless EJB) sends message even if transaction fails" Watch "JMS sender (Stateless EJB) sends message even if transaction fails" New topic

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

Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
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,
Mr sujeet khandelwal

Joined: Dec 21, 2011
Posts: 13

You can use user transaction to solve this issue.

javax.transaction.UserTransaction userTx;

try {
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();
// 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);
catch(Exception ex)
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Try EJBContext instead SessionContext.

Maybe it will work.

[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
I agree. Here's the link: http://aspose.com/file-tools
subject: JMS sender (Stateless EJB) sends message even if transaction fails
It's not a secret anymore!