Header Ads

ad

Distributed/Global Transaction Management


>> Global Transaction comes into picture when we deal with multiple databases.

>> It uses "2pc"(2 phase commit) protocol by involving multiple resources(eg. database) in a single transaction boundary.

2pc Protocol responsibility

>> 2pc protocol asks all databases if it can commit or not. If all databases reply yes then all databases data are committed. If any databases rises any error or exception then all databases rolled back the transaction as show below...

>> For this we need to use XA jdbc drivers, XA datasources and XA databases.

>> Oracle all versions, Mysql 5.x onwards are XA databases

>> To perform distributed/global transaction management, we use JTATransactionManager by supplying distributed transaction object and transaction service object.

>> We can get these 2, distributed transaction object and transaction service object from Jndi registry of application server or by using third party apis like Atomikos, Narayana etc..

JTATransactionManager example using Atomikos API
  1. <!-- Cfg Tx Service -->
  2. <bean id="txService" class="com.atomikos.icatch.jta.UserTransactionImp"/>
  3. <!-- Cfg Tx Mgmr -->
  4. <bean id="txMgmr" class="com.atomikos.icatch.jta.UserTransactionManager"/>
  1. <!-- Cfg JtaTransactionManager -->
  2. <bean id="jtaTxMgmr" class="org.springframework.transaction.jta.JtaTransactionManager">
  3.    <property name="userTransaction" ref="txService"/>
  4.    <property name="transactionManager" ref="txMgmr"/>
  5. </bean>


>> If application is standalone, it is recommended to use third party apis like Atomikos, Narayana etc..

>> If application is a web application, it is recommended to use Server Managed transaction..

Programs Implementing Atomikos API

SpringAspectJAOP-Distributed-Transaction-using-Annotations

SpringAspectJAOP-Distributed-Transaction-using-100p Code

 SpringAspectJAOP-Distributed-Transaction-using-Spring-Boot





No comments