Home
/
Unlabelled
/
AspectJAOP Distributed Transaction Management Program using 100pCode | Atomikos API
AspectJAOP Distributed Transaction Management Program using 100pCode | 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;
- }
- }
AOPConfig.java:
- package com.nt.config;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.transaction.jta.JtaTransactionManager;
- import com.atomikos.icatch.jta.UserTransactionImp;
- import com.atomikos.icatch.jta.UserTransactionManager;
- @Configuration
- @EnableTransactionManagement
- public class AOPConfig {
- @Bean(name="txService")
- public UserTransactionImp createTxService() {
- return new UserTransactionImp();
- }
- @Bean(name="txMgmr")
- public UserTransactionManager createTxMgmr() {
- return new UserTransactionManager();
- }
- @Bean(name="jtaTxMgmr")
- public JtaTransactionManager createJtaTxMgmr() {
- JtaTransactionManager jta=new JtaTransactionManager();
- jta.setUserTransaction(createTxService());
- jta.setTransactionManager(createTxMgmr());
- return jta;
- }
- }
ServiceConfig.java:
- package com.nt.config;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @ComponentScan(basePackages="com.nt.service")
- public class ServiceConfig {
- }
PersistenceConfig.java:
- package com.nt.config;
- import java.util.Properties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.core.JdbcTemplate;
- import com.atomikos.jdbc.AtomikosDataSourceBean;
- @Configuration
- @ComponentScan(basePackages="com.nt.dao")
- public class PersistenceConfig {
- @Bean(name="oracleXADS")
- public AtomikosDataSourceBean createAtomikosOracleDs() {
- AtomikosDataSourceBean ads=null;
- Properties props=null;
- ads=new AtomikosDataSourceBean();
- ads.setUniqueResourceName("oracleXA");
- ads.setXaDataSourceClassName("oracle.jdbc.xa.client.OracleXADataSource");
- props=new Properties();
- props.setProperty("databaseName", "xe");
- props.setProperty("user", "system");
- props.setProperty("password", "manager");
- props.setProperty("URL", "jdbc:oracle:thin:@localhost:1521:xe");
- ads.setXaProperties(props);
- ads.setMaxPoolSize(10);
- return ads;
- }
- @Bean(name="mysqlXADS")
- public AtomikosDataSourceBean createAtomikosMysqlDs() {
- AtomikosDataSourceBean ads=null;
- Properties props=null;
- ads=new AtomikosDataSourceBean();
- ads.setUniqueResourceName("mysqlXA");
- ads.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
- props=new Properties();
- props.setProperty("databaseName", "skydatabase");
- props.setProperty("user", "root");
- props.setProperty("password", "root");
- props.setProperty("URL", "jdbc:mysql://localhost:3306/skydatabase");
- ads.setXaProperties(props);
- ads.setMaxPoolSize(10);
- return ads;
- }
- @Bean(name="oracleJt")
- public JdbcTemplate createJtForOracle() {
- return new JdbcTemplate(createAtomikosOracleDs());
- }
- @Bean(name="mysqlJt")
- public JdbcTemplate createJtForMysql() {
- return new JdbcTemplate(createAtomikosMysqlDs());
- }
- }
AppConfig.java:
- package com.nt.config;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @ComponentScan(basePackageClasses={PersistenceConfig.class,ServiceConfig.class,AOPConfig.class})
- public class AppConfig {
- }
DtxTest.java:
- package com.nt.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import com.nt.config.AppConfig;
- import com.nt.service.BankService;
- public class DtxTest {
- public static void main(String[] args) {
- ApplicationContext ctx=null;
- BankService service=null;
- ctx=new AnnotationConfigApplicationContext(AppConfig.class);
- 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();
- }
- }
build.gradle:
- dependencies {
- // https://mvnrepository.com/artifact/org.springframework/spring-context-support
- compile group: 'org.springframework', name: 'spring-context-support', version: '5.1.5.RELEASE'
- // https://mvnrepository.com/artifact/org.springframework/spring-jdbc
- compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.5.RELEASE'
- // https://mvnrepository.com/artifact/org.aspectj/aspectjrt
- compile group: 'org.aspectj', name: 'aspectjrt', version: '1.9.2'
- // https://mvnrepository.com/artifact/org.aspectj/aspectjweaver
- compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.2'
- // https://mvnrepository.com/artifact/com.atomikos/atomikos-util
- compile group: 'com.atomikos', name: 'atomikos-util', version: '4.0.6'
- // https://mvnrepository.com/artifact/com.atomikos/transactions-jta
- compile group: 'com.atomikos', name: 'transactions-jta', version: '4.0.6'
- // https://mvnrepository.com/artifact/com.atomikos/transactions-api
- compile group: 'com.atomikos', name: 'transactions-api', version: '4.0.6'
- // https://mvnrepository.com/artifact/com.atomikos/transactions
- compile group: 'com.atomikos', name: 'transactions', version: '4.0.6'
- // https://mvnrepository.com/artifact/com.atomikos/transactions-jdbc
- compile group: 'com.atomikos', name: 'transactions-jdbc', version: '4.0.6'
- // https://mvnrepository.com/artifact/javax.transaction/jta
- compile group: 'javax.transaction', name: 'jta', version: '1.1'
- // https://mvnrepository.com/artifact/mysql/mysql-connector-java
- compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
- compile files('D:\\ojdbc6.jar')
- }
Post a Comment