开发者

How to perform XA transaction in EJB?

开发者 https://www.devze.com 2023-03-31 16:45 出处:网络
I have a MDB listening to particular topic. I have configured XA data source with jboss... I have set persistance .xml

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号