Do you know how I can get XAResource that is automaticaly enlist to my transaction when I use my entity manager ?

I use Bitronix, JPA, hibernate, my code works fine, but I don't want to rollback all my XAResources if one specific failed. I just want to delist it from the current transaction and others will be commited.

But for delist it of the current transaction, I need the object XaResource and I don't know how i get it with JPA/Bitronix. example of code :

transactionManager.begin();
try {
    (...)
    EntityManager em = emf.createEntityManager();
    (...)
    em.close();
} catch (Exception e) {
    // old version - rollback every XaResource in the current transaction
    //transactionManager.rollback();
    //new version wanted - rollbackonly this XaResource    
    transactionManager.getTransaction().delistResource(XaResource ...);
    throw e;
}
transactionManager.commit();

Thanks for your help.

有帮助吗?

解决方案

The A(tomicity) property of ACID doesn't allow such scenario. The transaction can only be successful if all data sources were able to commit.

I think the Command Pattern can help you with your problem. Let's say you want to update two data sources, and if one fails you still have control whether to undo the already executed commands.

If you never want to undo, then you don't need XA transactions. You just have to use Resource Local transactions, and if one data source fails, the rest could proceed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top