dcsimg
JTA rollback not working
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   njr_frens
Posted On:   Sunday, May 9, 2010 09:59 AM

I am using NetWeaver ApplicationServer I am trying to embed the JMS Transactions inside a JTA UserTransaction. Even after I rollback my JTA UserTransaction, the JMS operations are not rolling back. Is there any issue with my code? public class JMSSender extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; PrintWriter out; private XAQueueConnectionFactory queueConnectionFactory; private Queue queue; private QueueSession queueSession = null; private QueueSender queueSender = null; private TextMessage message = null; private XAQueueConnection queueConnection = null;    More>>

I am using NetWeaver ApplicationServer
I am trying to embed the JMS Transactions inside a JTA UserTransaction.

Even after I rollback my JTA UserTransaction, the JMS operations are not rolling back.

Is there any issue with my code?

			
public class JMSSender extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;

PrintWriter out;

private XAQueueConnectionFactory queueConnectionFactory;
private Queue queue;
private QueueSession queueSession = null;
private QueueSender queueSender = null;
private TextMessage message = null;
private XAQueueConnection queueConnection = null;

@Resource
UserTransaction utx;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
out = response.getWriter();

try {
utx.begin();

initQueue();
printQueue("Q Before");
publishMessage(false);

utx.rollback();
//utx.commit();
} catch (Exception e) {
e.printStackTrace(out);
try {
utx.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
} finally {
printQueue("Finally");
closeJMS();
}
}

public void publishMessage(boolean rollBackFlag) throws Exception {
int rand = new Random(100).nextInt();
for ( int i = 1; i < 5; i++) {
message = queueSession.createTextMessage();
message.setText("Current Time is (" + (new Date().getTime() + rand + i) + ") :: " + new Date());
out.println("
" + "Sending message :: " + message.getText());
queueSender.send(message);

if (rollBackFlag && (i == 3)) {
throw new Exception("Throwing Exception");
}
}
}

private void closeJMS() {
try {
if (queueSession != null) {
queueSession.close();
}
if (queueConnection != null) {
queueConnection.close();
}
} catch (Exception e) {
out.println("
");
e.printStackTrace(out);
}
}

public void printQueue(String desc) {
try {
out.println( "
" + desc + " Queue contents :: " );
QueueBrowser browser = queueSession.createBrowser(queue);
Enumeration en = browser.getEnumeration();
while (en.hasMoreElements()) {
TextMessage queueBrMessage = en.nextElement();
out .println( "
" + queueBrMessage.getText());
}
} catch (JMSException e) {
out.println("
");
e.printStackTrace(out);
}
}

public void initQueue() throws Exception {
InitialContext ctx = getInitialContext();

queueConnectionFactory = (XAQueueConnectionFactory) ctx.lookup("jmsfactory/default/VQConFactoryXA");
queue = (Queue) ctx.lookup("jmsqueues/default/VQueue");

queueConnection = queueConnectionFactory.createXAQueueConnection();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

queueSender = queueSession.createSender(queue);
queueConnection.start();
}

public InitialContext getInitialContext() throws Exception {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl");
properties.put(Context.PROVIDER_URL, "localhost:50004");
properties.put(Context.SECURITY_PRINCIPAL, "Administrator");
properties.put(Context.SECURITY_CREDENTIALS, "atlas123");
properties.put("force_remote", "true");
InitialContext context = new InitialContext(properties);
return context;
}
}
   <<Less

Re: JTA rollback not working

Posted By:   njr_frens  
Posted On:   Sunday, May 9, 2010 10:00 AM

I should not use JMS trasacted session, I need to use JTA Transaction only because I need to perform a distributed transaction
About | Sitemap | Contact