什么是分布式事务?在网上找了一段比较容易理解的"定义".

分布式事务是指事务的参与者、支持事务的服务器、资源管理器以及事务管理器分别位于分布系统的不同节点之上,在两个或多个网络计算机资源上访问并且更新数据,将两个或多个网络计算机的数据进行的多次操作作为一个整体进行处理。如不同银行账户之间的转账。

对于在项目中接触到JTA,大部分的原因是因为在项目中需要操作多个数据库,同时,可以保证操作的原子性,保证对多个数据库的操作一致性。

在正式的项目中应该用springMVC(struts)+spring+hibernate(jpa)+jta,目前,先用spring+jta来完成基本的测试框架。下面我们看看代码

applicationContext-jta.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- jotm 本地实例 -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> <!-- JTA事务管理器 -->
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm"></property>
</bean> <!-- XAPool配置,内部包含了一个XA数据源,对应sshdb数据库 -->
<bean id="db1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource">
<!-- 内部XA数据源 -->
<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://192.168.1.28:3306/sshdb?useUnicode=true&amp;characterEncoding=UTF-8" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="123456" />
</bean> <!-- 另一个XAPool配置,内部包含另一个XA数据源,对应babasport数据库 -->
<bean id="db2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://192.168.1.28:3306/babasport?useUnicode=true&amp;characterEncoding=UTF-8" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="123456" />
</bean> <!-- 配置访问sshdb数据源的Spring JDBC模板 -->
<bean id="sshdbTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="db1"></property>
</bean> <!-- 配置访问babasport数据源的Spring JDBC模板 -->
<bean id="babasportTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="db2"></property>
</bean>
</beans>

小注一下:spring-tx-3.2.4.jar里面竟然没有org.springframework.transaction.jta.JotmFactoryBean类,如果你可以选择spring-tx-2.5.6.jar,或者自己建立一下这个类。

接下来,看下dao层测试的代码

  1     @Resource(name = "txManager")
2 private JtaTransactionManager txManager;
9
10 protected JdbcTemplate babasport_jdbcTemplate;
11
12 /**
13 * sshdb sql jdbcTemplate
14 */
15 protected JdbcTemplate ssh_jdbcTemplate;
16
17 /**
18 * babasport sql jdbcTemplate
19 *
20 * @return
21 */
22 public JdbcTemplate getBabasport_jdbcTemplate() {
23 return babasport_jdbcTemplate;
24 }
25
26 public JdbcTemplate getSsh_jdbcTemplate() {
27 return ssh_jdbcTemplate;
28 }
29
30 @Resource(name = "babasportTemplate")
31 public void setBabasport_jdbcTemplate(JdbcTemplate babasport_jdbcTemplate) {
32 this.babasport_jdbcTemplate = babasport_jdbcTemplate;
33 }
34
35 @Resource(name = "sshdbTemplate")
36 public void setSsh_jdbcTemplate(JdbcTemplate ssh_jdbcTemplate) {
37 this.ssh_jdbcTemplate = ssh_jdbcTemplate;
38 }
39
40 /**
41 * 同时修改两个数据库的表中内容
42 *
43 * @throws RollbackException
44 */
45 public void updateMultiple() {
46
47 if (null == this.txManager) {
48 System.out.println("txManager为空");
49 return;
50 }
51
52 UserTransaction userTx = this.txManager.getUserTransaction();
53 if (null == userTx) {
54 System.out.println("userTx为空");
55 return;
56 }
57
58 try {
59
60 userTx.begin();
61
62 this.ssh_jdbcTemplate
63 .execute("update wyuser set password='wangyong1' where id=8");
64
65
66 this.babasport_jdbcTemplate
67 .execute("update brand set name='wangyong28' where code='14ac8d5b-d19c-40e9-97ea-d82dfbcd84c6'");
68
69 userTx.commit();
70 } catch (Exception e) {
71 System.out.println("捕获到异常,进行回滚" + e.getMessage());
72 e.printStackTrace();
73 try {
74 userTx.rollback();
75 } catch (IllegalStateException e1) {
76 System.out.println("IllegalStateException:" + e1.getMessage());
77 } catch (SecurityException e1) {
78 System.out.println("SecurityException:" + e1.getMessage());
79 } catch (SystemException e1) {
80 System.out.println("SystemException:" + e1.getMessage());
81 }
82 // System.out.println("sql语句操作失败");
83 }
84 }

如果,将后一条update语句故意写错,就会发现会执行rollback,同时,对上面一个语句的操作也不会生效。基本的简单框架就是这样。

其实,之前也测试了下spring+jpa+jta的框架模式,却发现,在建立model层实体类的时候会有问题,建立的entity类映射到所有的数据库中了,于是在jpa中利用属性<property name="packagesToScan" value="包名" />这种方式的确可以解决实体类entity的映射问题,不过貌似又出现其他问题,待研究.......

分布式事务操作之Spring+JTA的更多相关文章

  1. atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA)

    原文链接:http://iteye.blog.163.com/blog/static/1863080962012102945116222/   Atomikos TransactionsEssenti ...

  2. 【分布式事务】使用atomikos+jta解决分布式事务问题

    一.前言 分布式事务,这个问题困惑了小编很久,在3个月之前,就间断性的研究分布式事务.从MQ方面,数据库事务方面,jta方面.近期终于成功了,使用JTA解决了分布式事务问题.先写一下心得,后面的二级提 ...

  3. spring分布式事务学习笔记

    最近项目中使用了分布式事务,本文及接下来两篇文章总结一下在项目中学到的知识. 分布式事务对性能有一定的影响,所以不是最佳的解决方案,能通过设计避免最好尽量避免. 分布式事务(Distributed t ...

  4. j2ee中spring的分布式事务实现及解决方案

    1 java事务类型 Java事务的类型有三种:JDBC事务.JTA(Java Transaction API)事务.容器事务. 常见的容器事务如Spring事务,容器事务主要是J2EE应用服务器提供 ...

  5. 分布式事务(二)Java事务API(JTA)规范

    一.引子 既然出现了分布式场景(DTP模型), 大java也及时制定出一套规范来给各大应用服务器.数据库/mq等厂商使用,以方便管理互通--->JTA闪亮登场.JTA(Java Transact ...

  6. Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务

    一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...

  7. Spring Boot2.0之多数据源分布式事务问题

    分布式事务解决方案的问题, 分布式事务产生的原因: 多个不同的服务连接不同的数据源 ,做分布式事务的管理. 这种情况是连接两个数据源的情况,然后事务管理器是这样的 只管理了test02的这端业务代码. ...

  8. SpringCloud 分布式事务解决方案

    目录 TX-LCN分布式事务框架 TX-LCN分布式事务框架 随着互联化的蔓延,各种项目都逐渐向分布式服务做转换.如今微服务已经普遍存在,本地事务已经无法满足分布式的要求,由此分布式事务问题诞生. 分 ...

  9. 分布式事务(三)mysql对XA协议的支持

    系列目录 分布式事务(一)原理概览 分布式事务(二)JTA规范 分布式事务(三)mysql对XA协议的支持 分布式事务(四)简单样例 分布式事务(五)源码详解 分布式事务(六)总结提高 引子 从Mys ...

随机推荐

  1. RedHat6.5更新软件源

    今天在Red Hat上安装软件时,发现需要依赖软件,然而在用sudo yum指令时,出现了下面的错误: This system is not registered to Red Hat Subscri ...

  2. Support Vector Machine (3) : 再谈泛化误差(Generalization Error)

    目录 Support Vector Machine (1) : 简单SVM原理 Support Vector Machine (2) : Sequential Minimal Optimization ...

  3. make[2]: *** No rule to make target `/root/.pyenv/versions/anaconda3-2.4.0/lib/libpython3.5m.so', needed by `evaluation.so'. Stop.

    当出现No rule to make target ,肯定是Makefile有问题. 有的makefile是脚本生成的,你得看脚本的配置文件对不对. 我的是这个脚本生成的.发现是Pythondir的配 ...

  4. frp内网穿透配置

    frps配置 --------------------------------------------------------------------------------------------- ...

  5. Matlab melband的计算

    %% mel bankmelnum = 24;low = 0;high = 0.5;melbank=melbankm(melnum,fftsize,Fs,low,high,'m');%归一化mel滤波 ...

  6. {Reship}{Code}{CV}

    UIUC的Jia-Bin Huang同学收集了很多计算机视觉方面的代码,链接如下: https://netfiles.uiuc.edu/jbhuang1/www/resources/vision/in ...

  7. php中Content-type说明

    'hqx' -> 'application/mac-binhex40','cpt' -> 'application/mac-compactpro','doc' -> 'applica ...

  8. 刷CM7固件 乐padA1-07专用固件

    --------------------------------------------------------------------------------               前几天在版 ...

  9. unity5.0新功能-布料、动画系统

    原作者:只待苍霞 这一章讲一下布料系统, 这次的布料系统有很大的改良.Unity4中, 需要对SkinnedMeshRenderer使用SkinnedCloth, 或者对Cloth Renderer使 ...

  10. WCF Routing 服务

    WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...