阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Spring实现WebService分布式事务一致性

462次阅读
没有评论

共计 6918 个字符,预计需要花费 18 分钟才能阅读完成。

1. 分布式事物

分布式事务是指操作多个数据库之间的事务,为了保证事物的一致性,一般都采用 2 阶段提交的办法实现。

这里强调下一致性要求,如果追求强一致性就只能采用 JTA 事物实现。如果是最终一致性就不需要 JTA 实现了,可以采用异步消息队列实现。我这里用的是 spring 提供的 JTA 事物,因为这是个人习惯。

2. 服务端实现与配置

服务端采用 CXF 写 webservice 实现,关于 webservice 的实现与配置可以自行百度实现。我在服务端写了多个配置文件,其中一个是 webservice 的发布文件,一个是实现 DAO 层的配置,这里是不需要声明事物和对应切面的,但是一定要配置支持 XA 的数据源。JTA 事物的配置在客户端实现。我项目里数据源采用阿里的 druid。服务端的结果通过 json 传递给客户端。

服务端部分配置如下:

  <bean id=”helloServiceBean” class=”com.liuyu.service.impl.HelloServiceImpl”/>
  <jaxws:server id=”helloService” serviceClass=”com.liuyu.service.HelloServiceI” address=”/Hello”>
  <jaxws:serviceBean>                 
    <ref bean=”helloServiceBean”/>   
  </jaxws:serviceBean>
    </jaxws:server>
 3. 客户端测试
  客户端部分配置文件如下:
    <bean id=”atomikosTransactionManager” class=”com.atomikos.icatch.jta.UserTransactionManager”
  init-method=”init” destroy-method=”close”>
  <property name=”forceShutdown” value=”true” />
  </bean>
  <bean id=”atomikoSUSErTransaction” class=”com.atomikos.icatch.jta.UserTransactionImp”>
  <property name=”transactionTimeout” value=”300″ />
  </bean>
  <!– JTA 事务管理器 –>
  <bean id=”springTransactionManager”
  class=”org.springframework.transaction.jta.JtaTransactionManager”>
  <property name=”transactionManager” ref=”atomikosTransactionManager” />
  <property name=”userTransaction” ref=”atomikosUserTransaction” />
  </bean>
  <!– 事务管理 –>
  <tx:advice id=”txAdvice” transaction-manager=”springTransactionManager”>
  <tx:attributes>
  <tx:method name=”save*” propagation=”REQUIRED”/>
  <tx:method name=”add*” propagation=”REQUIRED”/>
  <tx:method name=”create*” propagation=”REQUIRED”/>
  <tx:method name=”insert*” propagation=”REQUIRED”/>
  <tx:method name=”update*” propagation=”REQUIRED”/>
  <tx:method name=”delete*” propagation=”REQUIRED”/>
  <tx:method name=”*” read-only=”true” />
  </tx:attributes>
  </tx:advice>
  <aop:config proxy-target-class=”true”>
  <aop:advisor advice-ref=”txAdvice” pointcut=”execution(* com.liuyu.test..*.*(..))” />
  </aop:config>
 
  客户端实现类部分如下:
 @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { “classpath*:applicationContext-client.xml”})
  @Transactional
  @TransactionConfiguration(defaultRollback = false)
  public class TestClient {
  @Autowired
  private HelloServiceI service;
  @Autowired
  private DemoServiceI demoService;
  @Test
  public void saveInfo() {
    service.insertHello(“2222”, “kkkkk”);
    demoService.insertHello(“2222”, “jjj”);
  }
  //@Test
  public void queryAllXA() {
    String str = demoService.queryAll();
    List<HashMap<String, Object>> list=JSON.parseObject(str, new TypeReference<List<HashMap<String,Object>>>(){});
    for(Map<String, Object> m:list){
    System.out.println(m.get(“id”).toString()+”  “+m.get(“username”).toString());
    }
  }
  }
  测试结果如下:
 [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING core version: 3.9.3
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.automatic_resource_registration = true
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.client_demarcation = false
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.threaded_2pc = false
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.serial_jta_transactions = true
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.serializable_logging = true
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.log_base_dir = .\
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.max_actives = 50
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.checkpoint_interval = 500
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.enable_logging = true
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.output_dir = .\
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.log_base_name = tmlog
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.max_timeout = 300000
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.tm_unique_name = 192.168.135.100.tm
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING java.naming.factory.initial = com.sun.jndi.rmi.registry.RegistryContextFactory
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING java.naming.provider.url = rmi://localhost:1099
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.service = com.atomikos.icatch.standalone.UserTransactionServiceFactory
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.force_shutdown_on_vm_exit = false
  [com.atomikos.icatch.config.imp.AbstractUserTransactionService]USING com.atomikos.icatch.default_jta_timeout = 10000
  [org.springframework.transaction.jta.JtaTransactionManager]Using JTA UserTransaction: com.atomikos.icatch.jta.UserTransactionImp@101b7cf
  [org.springframework.transaction.jta.JtaTransactionManager]Using JTA TransactionManager: com.atomikos.icatch.jta.UserTransactionManager@1cab519
  一月 20, 2015 4:44:56 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  信息: Creating Service {http://service.liuyu.com/}HelloServiceIService from class com.liuyu.service.HelloServiceI
  一月 20, 2015 4:44:57 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
  信息: Creating Service {http://service.demo.liuyu.com/}DemoServiceIService from class com.liuyu.demo.service.DemoServiceI
  [com.atomikos.icatch.imp.thread.TaskManager]THREADS: using JDK thread pooling…
  [com.atomikos.icatch.imp.BaseTransactionManager]createCompositeTransaction (300000): created new ROOT transaction with id 192.168.135.100.tm0000100002
  [org.springframework.test.context.transaction.TransactionContext]Began transaction (1) for test context [DefaultTestContext@f2ed42 testClass = TestClient, testInstance = com.liuyu.test.TestClient@b691c6, testMethod = saveInfo@TestClient, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1c63a8 testClass = TestClient, locations = ‘{classpath*:applicationContext-client.xml}’, classes = ‘{}’, contextInitializerClasses = ‘[]’, activeProfiles = ‘{}’, propertySourceLocations = ‘{}’, propertySourceProperties = ‘{}’, contextLoader = ‘org.springframework.test.context.support.DelegatingSmartContextLoader’, parent = [null]]]; transaction manager [org.springframework.transaction.jta.JtaTransactionManager@1d2d7c9]; rollback [false]
  [com.atomikos.icatch.imp.CompositeTransactionImp]commit() done (by application) of transaction 192.168.135.100.tm0000100002

Struts2 整合 Spring 方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0 集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm

运用 Spring 注解实现 Netty 服务器端 UDP 应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企业应用开发实战 PDF 完整高清扫描版 + 源代码 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的详细介绍 :请点这里
Spring 的下载地址 :请点这里 

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计6918字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19350
评论数
4
阅读量
7959195
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛 NAS 硬件 02:某鱼 6 张左右就可拿下 5 盘位的飞牛圣体 NAS 前言 大家好,我是星...
免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

  免费无广告!这款跨平台 AI RSS 阅读器,拯救你的信息焦虑 在算法推荐主导信息流的时代,我们...
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
星哥带你玩飞牛NAS-7:手把手教你免费内网穿透-Cloudflare tunnel

星哥带你玩飞牛NAS-7:手把手教你免费内网穿透-Cloudflare tunnel

星哥带你玩飞牛 NAS-7:手把手教你免费内网穿透 -Cloudflare tunnel 前言 大家好,我是星...
你的云服务器到底有多强?宝塔跑分告诉你

你的云服务器到底有多强?宝塔跑分告诉你

你的云服务器到底有多强?宝塔跑分告诉你 为什么要用宝塔跑分? 宝塔跑分其实就是对 CPU、内存、磁盘、IO 做...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
星哥带你玩飞牛NAS硬件 01:捡垃圾的最爱双盘,暴风二期矿渣为何成不老神话?

星哥带你玩飞牛NAS硬件 01:捡垃圾的最爱双盘,暴风二期矿渣为何成不老神话?

星哥带你玩飞牛 NAS 硬件 01:捡垃圾的最爱双盘,暴风二期矿渣为何成不老神话? 前言 在选择 NAS 用预...
还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手! 前言 对于个人开发者、建站新手或是想搭建测试站点的从业者...
让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级 大家好,我是星哥,之前写了一篇文章 自己手撸一...
开发者福利:免费 .frii.site 子域名,一分钟申请即用

开发者福利:免费 .frii.site 子域名,一分钟申请即用

  开发者福利:免费 .frii.site 子域名,一分钟申请即用 前言 在学习 Web 开发、部署...