Two-phase commit (2PC) configuration with Atomikos

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 22:44:53

To get the behaviour you expect you need to perform your DAO calls inside the same transaction, for example, as follows:

final StockBo stockBo = (StockBo)appContext.getBean("stockBo1");
final StockBo stockBo2 = (StockBo)appContext.getBean("stockBo2");
TransactionTemplate tx = new TransactionTemplate(appContext.getBean(PlatformTransactionManager.class);

tx.execute(new TransactionCallback<Void>() {
    public Void doInTransaction(TransactionStatus ts) {
        /** insert **/
        Stock stock = new Stock();
        stock.setStockCode("7668");
        stock.setStockName("HAIO");
        stockBo.save(stock);

        Stock stock1 = new Stock();
        //stock1.setStockCode("1668"); **Commented to fail the second db insert**
        stock1.setStockName("AAIO");
        stockBo2.save(stock1);

       return null;
    }
});

Also, I think you don't need the following lines since you configure Hibernate with Spring:

<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop> 
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
<prop key="hibernate.connection.release_mode">on_close</prop>  

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!