aspose file tools*
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 8 in Action this week in the Java 8 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
Author

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,
Nitin
Mr sujeet khandelwal
Greenhorn

Joined: Dec 21, 2011
Posts: 13
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

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]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JMS sender (Stateless EJB) sends message even if transaction fails
 
Similar Threads
Glassfish: How do I set endpointExceptionRedeliveryAttempts for a standalone JMS client?
MDB deployment on JBoss
Queue - Unlimited Attempts At Delivery
Strange behaviour of MDB and EJB
Error when deploying an EAR on JBoss 5.1.0