spring整合atomikos实现分布式事务的方法示例

网友投稿 302 2023-01-06


spring整合atomikos实现分布式事务的方法示例

前言

Atomikos 是一个为java平台提供增值服务的并且开源类事务管理器,主要用于处理跨数据库事务,比如某个指令在A库和B库都有写操作,业务上要求A库和B库的写操作要具有原子性,这时候就可以用到atomikos。笔者这里整合了一个spring和atomikos的demo,并且通过案例演示说明atomikos的作用。

准备工作

开发工具:idea

数据库:mysql , oracle

正文

源码地址: https://github.com/qw870602/atomikos

演示原理:通过在两个库的写操作之间人为制造异常来观察数据库是否回滚

演示步骤:1.正常写操作,观察数据库值的变化情况

2.在写操作语句之间制造异常,观察数据库值的变化情况

项目结构

从web.xml中可以知道,容器只加载了appliactionContext.xml,剩下的配置文件除了database.properties外都是无用文件,所以大家如果要在项目中配置的话,仅需要把appliactionContext.xml中关于atomikos的部分新增到自己项目中就OK了

appliactionContext.xml

xmlns="http://springframework.org/schema/beans"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:p="http://springframework.org/schema/p"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

${mysql.qa.db.url}

${mysql.qa.db.user}

${mysql.qa.db.password}

${oracle.qa.db.url}

${oracle.qa.db.user}

${oracle.qa.db.password}

classpath*:/dao/*.xml

classpath*:/daodev/*.xml

xmlns="http://springframework.org/schema/beans"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:p="http://springframework.org/schema/p"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

${mysql.qa.db.url}

${mysql.qa.db.user}

${mysql.qa.db.password}

${oracle.qa.db.url}

${oracle.qa.db.user}

${oracle.qa.db.password}

classpath*:/dao/*.xml

classpath*:/daodev/*.xml

适用JUnit4进行单元测试

package com.xy.controller;

import com.xy.daodev.TransactionTestService;

import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContZOkDQjnextConfiguration(locations = {"classpath:applicationContext.xml"})

public class TransactionTestMain extends AbstractJUnit4SpringContextTests {

@Autowired

private TransactionTestService transactionTestService;

/**

* 在同一事务有多个数据源

*/

@Test

public void multipleDataSource2() {

transactionTestService.updateMultipleDataSource("1","1", 100L,"1.6");

}

}

业务实现,当前没有异常操作

@Service

public class TransactionTestServiceImpl implements TransactionTestService {

@Autowired

@Qualifier("mysqlTransactionTestDao")

private MysqlTransactionTestDao mysqlTransactionTestDao;

@Autowired

@Qualifier("transactionTestDao")

private TransactionTestDao transactionTestDao;

/**

* 在同一事务有多个数据源

*/

@Override

@Transactional

public void updateMultipleDataSource(String deUserId, String http://inUserid, long money,String str) {

// 账户1转出操作

mysqlTransactionTestDao.decreaseMoney(deUserId, money);

//Integer.parseInt(str);

// 账户2转入操作

transactionTestDao.increaseMoney(inUserid, money);

}

}

mysql模拟金额转出,oracle模拟金额转入

UPDATE fx1 SET amount=amount - #{1,jdbcType=BIGINT} WHERE id=#{0,jdbcType=VARCHAR}

UPDATE fx1 SET amount=amount + #{1,jdbcType=BIGINT} WHERE id=#{0,jdbcType=VARCHAR}

mysql初始金额

oracle初始金额

执行正常操作

mysql当前金额

oracle当前金额

将被屏蔽的制造异常的代码打开

public void updateMultipleDataSource(String deUserId, String inUserid, long money,String str) {

// 账户1转出操作

mysqlTransactionTestDao.decreaseMoney(deUserId, money);

Integer.parseInt("skg");

// 账户2转入操作

transactionTestDao.increaseMoney(inUserid, money);

}

发现mysql和oracle的当前金额都没有变化,说明事务回滚成功,查看日志

发现控制台打印出了异常信息,并且atomikos调用了rollback()方法,从日志也证实了回滚成功。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:最新接口测试工具图片(最新接口测试工具图片大全)
下一篇:静态类能实现接口吗(静态类和接口的区别)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~