File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes transaction in mdb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "transaction in mdb" Watch "transaction in mdb" New topic
Author

transaction in mdb

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
i write a message driven bean,that monitor the weblogic message queue,when a "Order" object is witten to the queue,the mdb get it and write it to a entity bean "Orderinfo".all of above logic is within the "onMessage" method of the mdb.
i want to encapsulate the flow in a transaction,see my code snippet of the onMessage method:

ObjectMessage objMsg = (ObjectMessage) msg;
OrderVO orderVO = (OrderVO) objMsg.getObject();
System.out.println(orderVO.booklist);
OrderinfoHome orderinfoHome = (OrderinfoHome) ctx.lookup(
"java:/comp/env/orderinfo");
Orderinfo orderinfo = orderinfoHome.create(orderVO.orderID);
orderinfo.setAddress(orderVO.address);
orderinfo.setCustname(orderVO.custName);
orderinfo.setEmail(orderVO.email);
orderinfo.setBooklist(orderVO.booklist);
orderinfo.setPrice(new BigDecimal(orderVO.price));

and deploy descriptor snippet(ejb-jar.xml):

<assembly-descriptor>
.............
............
<container-transaction>
<method>
<ejb-name>orderMDB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

i think during this transaction,there are two action:geting the object from the queue and saving it to entity bean.in order to test the transaction,i modify the jndi name of entity bean in the code to a WRONG one.redeploy my program,and send a message to the queue,the mdb is activated,then the exception is thrown because of the wrong jndi name.after that,i check the message queue,find that it is empty.why?i think if the second action of the transaction is fail,the transaction should roll back,the message should be send BACK to the queue.
i also ty to use the "javax.transaction.UserTransaction" in the onMessage method,but the follwing exception is thrown:

javax.transaction.NotSupportedException: Another transaction is associated with this thread.................................

who can help me,if any wrong with me,and how to use the transaction with the message driven bean?
thank you.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Isn't the exception thrown by the JNDI lookup not a system exception? Anyway, I would explicitly call setRollbackOnly() just to be sure (and have the code communicate better that the transaction is being rolled back).
Btw. You can only use container-managed transactions with MDBs -- that's why you couldn't use UserTransaction.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by Lasse Koskela:
Btw. You can only use container-managed transactions with MDBs -- that's why you couldn't use UserTransaction.

Actually, you can use BMT with MDBs also. However, that still doesn't allow you to use UserTransaction if your MDB is marked as CMT.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Duh. Can't believe I just passed SCBCD
Brian Tinnel
Ranch Hand

Joined: Aug 25, 2003
Posts: 69
In WebLogic, unless you configure it otherwise, a message that is rolled back is dropped. That is because the default redelivery limit is -1 (meaning no retry) and the error destination is (None). Assuming you set up an Error Destination (just a different queue), your message should appear there after you roll it back. If you set the redelivery limit to a value X, your MDB should be called X times and then the message will appear in the Error Destination.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transaction in mdb
 
Similar Threads
I get message to queue, but not to MDB Bean. So close yet so far!
WL guru's - message bean not receiving messages from JMS
How to programmatically find if EJB is operating in transaction mode
Message lost or are not pulled by MDB
How to form a MDB with the topic?