Header Ads

ad

AspectJAOP Distributed Transaction Management Program using Spring Boot | Atomikos API

Deposite.java:
  1. package com.nt.dao;
  2. public interface Deposite {
  3. public int deposite(int acno,int amount);
  4. }


DepositeImpl.java:
  1. package com.nt.dao;

  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.jdbc.core.JdbcTemplate;
  5. import org.springframework.stereotype.Repository;

  6. @Repository("depositeDAO")
  7. public class DepositeImpl implements Deposite {
  8. private static final String DEPOSITE_QUERY="UPDATE DTX_ACCOUNT SET BALANCE=BALANCE+? WHERE ACNO=?";
  9. @Autowired
  10. @Qualifier(value="mysqlJt")
  11. private JdbcTemplate jt;
  12. @Override
  13. public int deposite(int acno, int amount) {
  14. int count=0;
  15. count=jt.update(DEPOSITE_QUERY, amount,acno);
  16. return count;
  17. }
  18. }
Withdraw.java:
  1. package com.nt.dao;
  2. public class Withdraw {
  3.     public int withdraw(int acno,int amount);
  4. }

WithdrawImpl.java:
  1. package com.nt.dao;

  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.jdbc.core.JdbcTemplate;
  5. import org.springframework.stereotype.Repository;

  6. @Repository("withdrawDAO")
  7. public class WithdrawImpl implements Withdraw {
  8. private static final String WITHDRAW_QUERY="UPDATE  DTX_ACCOUNT SET BALANCE=BALANCE-? WHERE ACNO=?";
  9. @Autowired
  10. @Qualifier(value="oracleJt")
  11. private JdbcTemplate jt;
  12. @Override
  13. public int withdraw(int acno, int amount) {
  14. int count=0;
  15. count=jt.update(WITHDRAW_QUERY, amount,acno);
  16. return count;
  17. }
  18. }

BankService.java:
  1. package com.nt.service;
  2. public interface BankService {
  3.      public boolean transferMoney(int srcAcno, int destAcno, int amt)throws Exception;
  4. }

BankServiceImpl.java:
  1. package com.nt.service;

  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Propagation;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import com.nt.dao.Deposite;
  7. import com.nt.dao.Withdraw;

  8. @Service("bankService")
  9. public class BankServiceImpl implements BankService {
  10. @Autowired
  11. private Withdraw withDAO;
  12. @Autowired
  13. private Deposite depoDAO;
  14. @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
  15. public boolean transferMoney(int srcAcno, int destAcno, int amt)throws Exception {
  16. int wFlag=0,dFlag=0;
  17. boolean status=false;
  18. wFlag=withDAO.withdraw(srcAcno, amt);
  19. dFlag=depoDAO.deposite(destAcno, amt);
  20. if(wFlag==0 || dFlag==0) {
  21. status=false;
  22. throw new Exception("Transaction Failed....");
  23. }
  24. else {
  25. status=true;
  26. }
  27. return status;
  28. }
  29. }

AOPConfig.java:
  1. package com.nt.config;

  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.transaction.annotation.EnableTransactionManagement;
  5. import org.springframework.transaction.jta.JtaTransactionManager;
  6. import com.atomikos.icatch.jta.UserTransactionImp;
  7. import com.atomikos.icatch.jta.UserTransactionManager;

  8. @Configuration
  9. public class AOPConfig {
  10. @Bean(name="txService")
  11. public UserTransactionImp createTxService() {
  12. return new UserTransactionImp();
  13. }
  14. @Bean(name="txMgmr")
  15. public UserTransactionManager createTxMgmr() {
  16. return new UserTransactionManager();
  17. }
  18. @Bean(name="jtaTxMgmr")
  19. public JtaTransactionManager createJtaTxMgmr() {
  20. JtaTransactionManager jta=new JtaTransactionManager();
  21. jta.setUserTransaction(createTxService());
  22. jta.setTransactionManager(createTxMgmr());
  23. return jta;
  24. }
  25. }

ServiceConfig.java:
  1. package com.nt.config;

  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;

  4. @Configuration
  5. @ComponentScan(basePackages="com.nt.service")
  6. public class ServiceConfig {
  7. }


PersistenceConfig.java:
  1. package com.nt.config;

  2. import java.util.Properties;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import com.atomikos.jdbc.AtomikosDataSourceBean;

  8. @Configuration
  9. @ComponentScan(basePackages="com.nt.dao")
  10. public class PersistenceConfig {
  11. @Bean(name="oracleXADS")
  12. public AtomikosDataSourceBean createAtomikosOracleDs() {
  13. AtomikosDataSourceBean ads=null;
  14. Properties props=null;
  15. ads=new AtomikosDataSourceBean();
  16. ads.setUniqueResourceName("oracleXA");
  17. ads.setXaDataSourceClassName("oracle.jdbc.xa.client.OracleXADataSource");
  18. props=new Properties();
  19. props.setProperty("databaseName", "xe");
  20. props.setProperty("user", "system");
  21. props.setProperty("password", "manager");
  22. props.setProperty("URL", "jdbc:oracle:thin:@localhost:1521:xe");
  23. ads.setXaProperties(props);
  24. ads.setMaxPoolSize(10);
  25. return ads;
  26. }
  27. @Bean(name="mysqlXADS")
  28. public AtomikosDataSourceBean createAtomikosMysqlDs() {
  29. AtomikosDataSourceBean ads=null;
  30. Properties props=null;
  31. ads=new AtomikosDataSourceBean();
  32. ads.setUniqueResourceName("mysqlXA");
  33. ads.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
  34. props=new Properties();
  35. props.setProperty("databaseName", "skydatabase");
  36. props.setProperty("user", "root");
  37. props.setProperty("password", "root");
  38. props.setProperty("URL", "jdbc:mysql://localhost:3306/skydatabase");
  39. ads.setXaProperties(props);
  40. ads.setMaxPoolSize(10);
  41. return ads;
  42. }
  43. @Bean(name="oracleJt")
  44. public JdbcTemplate createJtForOracle() {
  45. return new JdbcTemplate(createAtomikosOracleDs());
  46. }
  47. @Bean(name="mysqlJt")
  48. public JdbcTemplate createJtForMysql() {
  49. return new JdbcTemplate(createAtomikosMysqlDs());
  50. }
  51. }

AppConfig.java:
  1. package com.nt.config;

  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;

  4. @Configuration
  5. @ComponentScan(basePackageClasses={PersistenceConfig.class,ServiceConfig.class,AOPConfig.class})
  6. public class AppConfig {
  7. }

DtxTest.java:
  1. package com.nt.test;

  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.annotation.Import;
  7. import org.springframework.context.support.AbstractApplicationContext;
  8. import com.nt.config.AppConfig;
  9. import com.nt.service.BankService;

  10. @SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
  11. @Import(AppConfig.class)
  12. public class DtxTest {
  13. public static void main(String[] args) {
  14. ApplicationContext ctx=null;
  15. BankService service=null;
  16. ctx=SpringApplication.run(DtxTest.class, args);
  17. service=ctx.getBean("bankService",BankService.class);
  18. try {
  19. System.out.println(service.transferMoney(101, 102, 2000));
  20. }
  21. catch(Exception e) {
  22. System.out.println("Transaction failed,,Internal Problems.....");
  23. e.printStackTrace();
  24. }
  25. ((AbstractApplicationContext) ctx).close();
  26. }
  27. }



No comments