Home
/
Unlabelled
/
Spring AspectJAOP Distributed Transaction Management Program using Annotations | Atomikos API
Spring AspectJAOP Distributed Transaction Management Program using Annotations | Atomikos API
- package com.nt.dao;
- public interface Deposite {
- public int deposite(int acno,int amount);
- }
DepositeImpl.java:
- package com.nt.dao;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Repository;
- @Repository("depositeDAO")
- public class DepositeImpl implements Deposite {
- private static final String DEPOSITE_QUERY="UPDATE DTX_ACCOUNT SET BALANCE=BALANCE+? WHERE ACNO=?";
- @Autowired
- @Qualifier(value="mysqlJt")
- private JdbcTemplate jt;
- @Override
- public int deposite(int acno, int amount) {
- int count=0;
- count=jt.update(DEPOSITE_QUERY, amount,acno);
- return count;
- }
- }
Withdraw.java:
- package com.nt.dao;
- public class Withdraw {
- public int withdraw(int acno,int amount);
- }
WithdrawImpl.java:
- package com.nt.dao;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Repository;
- @Repository("withdrawDAO")
- public class WithdrawImpl implements Withdraw {
- private static final String WITHDRAW_QUERY="UPDATE DTX_ACCOUNT SET BALANCE=BALANCE-? WHERE ACNO=?";
- @Autowired
- @Qualifier(value="oracleJt")
- private JdbcTemplate jt;
- @Override
- public int withdraw(int acno, int amount) {
- int count=0;
- count=jt.update(WITHDRAW_QUERY, amount,acno);
- return count;
- }
- }
BankService.java:
- package com.nt.service;
- public interface BankService {
- public boolean transferMoney(int srcAcno, int destAcno, int amt)throws Exception;
- }
BankServiceImpl.java:
- package com.nt.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import com.nt.dao.Deposite;
- import com.nt.dao.Withdraw;
- @Service("bankService")
- public class BankServiceImpl implements BankService {
- @Autowired
- private Withdraw withDAO;
- @Autowired
- private Deposite depoDAO;
- @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
- public boolean transferMoney(int srcAcno, int destAcno, int amt)throws Exception {
- int wFlag=0,dFlag=0;
- boolean status=false;
- wFlag=withDAO.withdraw(srcAcno, amt);
- dFlag=depoDAO.deposite(destAcno, amt);
- if(wFlag==0 || dFlag==0) {
- status=false;
- throw new Exception("Transaction Failed....");
- }
- else {
- status=true;
- }
- return status;
- }
- }
persistence-beans.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
- <!-- DataSource properties for Oracle -->
- <bean id="oracleXADS" class="com.atomikos.jdbc.AtomikosDataSourceBean">
- <property name="uniqueResourceName" value="oracleXA"/>
- <property name="xaDataSourceClassName" value="oracle.jdbc.xa.client.OracleXADataSource"/>
- <property name="xaProperties">
- <props>
- <prop key="databaseName">xe</prop>
- <prop key="user">system</prop>
- <prop key="password">manager</prop>
- <prop key="URL">jdbc:oracle:thin:@localhost:1521:xe</prop>
- </props>
- </property>
- <property name="poolSize" value="10"/>
- </bean>
- <!-- DataSource properties for MySQL -->
- <bean id="mysqlXADS" class="com.atomikos.jdbc.AtomikosDataSourceBean">
- <property name="uniqueResourceName" value="mysqlXA"/>
- <property name="xaDataSourceClassName" value="com.mysql.cj.jdbc.MysqlXADataSource"/>
- <property name="xaProperties">
- <props>
- <prop key="databaseName">skydatabase</prop>
- <prop key="user">root</prop>
- <prop key="password">root</prop>
- <prop key="URL">jdbc:mysql:///skydatabase</prop>
- </props>
- </property>
- <property name="poolSize" value="10"/>
- </bean>
- <!-- JdbcTemplate cfg -->
- <bean id="oracleJt" class="org.springframework.jdbc.core.JdbcTemplate">
- <constructor-arg ref="oracleXADS"/>
- </bean>
- <bean id="mysqlJt" class="org.springframework.jdbc.core.JdbcTemplate">
- <constructor-arg ref="mysqlXADS"/>
- </bean>
- <!-- combine class with cfg -->
- <context:component-scan base-package="com.nt.dao"/>
- </beans>
service-beans.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
- <context:component-scan base-package="com.nt.service"/>
- </beans>
aop-beans.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
- <!-- Cfg Tx Service -->
- <bean id="txService" class="com.atomikos.icatch.jta.UserTransactionImp"/>
- <!-- Cfg Tx Mgmr -->
- <bean id="txMgmr" class="com.atomikos.icatch.jta.UserTransactionManager"/>
- <!-- Cfg JtaTransactionManager -->
- <bean id="jtaTxMgmr" class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="userTransaction" ref="txService"/>
- <property name="transactionManager" ref="txMgmr"/>
- </bean>
- <!-- Activate TxMgmr -->
- <tx:annotation-driven transaction-manager="jtaTxMgmr"/>
- </beans>
applicationContext.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <import resource="persistence-beans.xml"/>
- <import resource="service-beans.xml"/>
- <import resource="aop-beans.xml"/>
- </beans>
- package com.nt.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.nt.service.BankService;
- public class DTxTest {
- public static void main(String[] args) {
- ApplicationContext ctx=null;
- BankService service=null;
- ctx=new ClassPathXmlApplicationContext("com/nt/cfgs/applicationContext.xml");
- service=ctx.getBean("bankService",BankService.class);
- try {
- System.out.println(service.transferMoney(101, 102, 2000));
- }
- catch(Exception e) {
- System.out.println("Transaction failed....Internal Problems.....");
- e.printStackTrace();
- }
- ((AbstractApplicationContext) ctx).close();
- }
- }
Post a Comment