简体   繁体   中英

Distributed Transaction management in Core Java application

Is it possible to use distributed transaction manager in core java application which does not run inside any container?

I have an application which reads message from JMS queue and writes it to the database. If writing to the database fails then complete transaction should roll back. I am thinking of using JOTM, but not sure how to configure it. Also not sure if that kind of configuration is possible at all, as the application does not run in any server. Any suggestion will really be helpful. Thanks in advance.

This Spring blog entry explains how to configure JTA outside of container. JOTM is one of the options described.

For your case, it would be simpler to just use ChainedTransactionManager to simulate JTA. You get nearly the same effect with minimal Spring configuration. Configure as below, and then specify @Transactional(value="chainedTransactionManager") on your listener.

@Bean 
JmsTransactionManager jmsTransactionManager(ConnectionFactory connectionFactory) {
    JmsTransactionManager manager = new JmsTransactionManager();
    manager.setConnectionFactory(connectionFactory);
    return manager;
}

@Bean 
JpaTransactionManager jpaTransactionManager() {
    JpaTransactionManager manager = new JpaTransactionManager();
    return manager;
}

//Encapsulating TM used to commit/rollback JMS and JPA together, without overhead of JTA.  Note that JMS should be listed first, as transactions commit in reverse order and JMS less likely to fail.
@Bean 
ChainedTransactionManager chainedTransactionManager(JmsTransactionManager jmsTransactionManager, JpaTransactionManager jpaTransactionManager){
    ChainedTransactionManager manager = new ChainedTransactionManager(jmsTransactionManager, jpaTransactionManager);
    return manager;
}

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