BEA Weblogic Question:
Download Questions PDF

How do I use JTA transactions within an MDB?

Answer:

In the ejb-jar.xml file, define the transaction type as Container and the trans-attribute as Required, as in the following example:
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>MDB</ejb-name>
<ejb-class>MDB</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MDB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>
Required
</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

To rollback a transaction, you can either use the WebLogic extension TXHelper or you can use the MDB context as in the following code examples:
UserTransaction ut =
weblogic.transaction.TXHelper.getUserTransaction();
ut.setRollbackOnly();
or
private MessageDrivenContext context;
public void setMessageDrivenContext(
MessageDrivenContext mycontext) {
context = mycontext;
}
public void onMessage(Message msg) {
try { // some logic
}
catch(Exception e) {
System.out.println("MDB doing rollback");
context.setRollbackOnly();
}

Download BEA Weblogic Interview Questions And Answers PDF

Previous QuestionNext Question
Can an MDB be a message producer or both a producer and consumer?Can the messaging bridge handle two-phase or global transactions between separate WebLogic Server domains or between different releases?