简体   繁体   中英

How to perform XA transaction in EJB?

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

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!
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM