3

I have a MDB listening to particular topic.

I have configured XA data source with jboss...

I have set persistance .xml

<persistence-unit name="jpa" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
 <jta-data-source>java:jdbc/BKS_DataSource</jta-data-source> 
<class>com.jms.mdb.SampleData</class>    
<properties>

    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

    <property name="hibernate.hbm2ddl.auto" value="update"/>

    <property name="hibernate.show_sql" value="true"/>
    <!--
    <property name="hibernate.format_sql" value="true"/>
    -->
</properties>
</persistence-unit> 

And whenever I receive something I just execute this code in MDB

@PersistenceContext
EntityManager em = null;

public void onMessage(Message message) {
    try {

        LoggingEvent event = (LoggingEvent)((ObjectMessage)message).getObject();
        System.out.println("Received something11");
        SampleData s= new SampleData();
        s.setMessage(event.getLoggerName());
        em.persist(s);

        System.out.println("Persisted");

        //Create.main(null);
    } catch (JMSException e) {
        e.printStackTrace();

    }     
}

So basically I need to perform Two phase commit transaction...So I want to know what should I have to do perform XA transaction....Plus I want to do it only on Java EE 5

Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
user796666
  • 91
  • 3
  • 11

1 Answers1

1

If your data source is configured to be an XA data source, then all you need to do is to annotate your MDB with the appropriate transaction management annotations:

@MessageDriven
@TransactionManagement(CONTAINER)
@TransactionAttribute(REQUIRED)
public class MyMDB implements MessageListener {

 public void onMessage(Message message) {
   // Hello, message!
 }

}
Behrang
  • 46,888
  • 25
  • 118
  • 160
  • Thank you so much for the answer..N I was just curious how it handles rollback? – user796666 Aug 30 '11 at 04:46
  • You mean how you can rollback the transaction or what happens when you roll it back? – Behrang Aug 30 '11 at 05:14
  • Yeah what happens when rollback occurs? n is it possible to control rollback? – user796666 Aug 30 '11 at 05:24
  • When the transaction is rolled back the incoming message is getting rolled back into the MDB's destination (queue/topic). Runtime exceptions roll back the transactions. To make a checked exception roll back the transaction you should annotate it with `ApplicationException` and set `rollback` to `true`. – Behrang Aug 30 '11 at 05:57
  • To explicitly rollback the transaction you can use `MessageDrivenContext`'s `setRollbackOnly` method. – Behrang Aug 30 '11 at 06:01