zoukankan      html  css  js  c++  java
  • 全局事务/分布式事务 (Global Transaction/ A distributed transaction)之我见

    这里参考的是Oracle对于XA的支持,其他的应该雷同吧。。。

    1个分布式事务由多个行为在不同的数据库上执行,1个分布式事务的执行成功意味着相关数据库上的行为执行均成功。“XA协定”(http://www.opengroup.org/public/pubs/catalog/c193.htm)就支持分布式的事务,具备能力,让多个独立的事务资源参加全局的事务。事务资源通常是RDBMSs,不过也可以是其它种类的资源。

    一个典型的全局性事务的架构如下,通常来说TM会集成在Application Server(例如weblogic server)中。这种TM也叫做external TM,区别于在MySQL DBMS或者Oracle DBMS中的管理本地事务的TM

     

     

     

    资源管理器(RM):用户提供通向事务的途径。数据库服务器(例如上面的Oracal DBMS)是一个种资源管理器。该管理器必须提交or回滚由RM管理的事务。

    事务管理器(TM):用于协调作为一个分布式事务的一部分事务。通常XA的相关操作都在这里进行,而对于Client而言是透明的,TM(或许是个进程)通常是由TPM( transaction processing monitor,Texudo就有这个组件,所以Texudo也就本能地支持了全局事务)提供。

     

    对于Client App而言,所有的Global Transaction都应该通过TM进行(在ORACLE中,是名字为TX的一组接口函数),TM再与RM通过XA接口(Oracle有提供这组函数)进行接洽。而所有的普通的针对同一个数据库的事务可以直接通过Native Interface进行。

    在Oracle的文档里,一个Global Transaction被分为多个Branch。A branch is a unit of work contained within one RM. In the case of Oracle Database, each branch maps to a local transaction inside the database server.

    理解全局性事务的关键是理解两阶段提交:

    The Oracle XA library interface follows the two-phase commit protocol. The sequence of events is as follows:

    1. In the prepare phase, the TM asks each RM to guarantee that it can commit any part of the transaction. If this is possible, then the RM records its prepared state and replies affirmatively to the TM. If it is not possible, then the RM may roll back any work, reply negatively to the TM, and forget about the transaction. The protocol allows the application, or any RM, to roll back the transaction unilaterally until the prepare phase completes.
    2. In phase two, the TM records the commit decision and issues a commit or rollback to all RMs participating in the transaction. TM can issue a commit for an RM only if all RMs have replied affirmatively to phase one.

    下面的一个例子是从Oracle的JDBC文档里搞出来的。基本上讲清楚了整个过程。特别注意的是,Note that for simplicity, this example combines code that would typically be in a middle tier with code that would typically be in a transaction manager (such as the XA resource method invocations and the creation of transaction IDs).

    Oracle XA Code Sample 最底下一段

     

    关于MySQL全局性事务的一些限制,http://jishu.zol.com.cn/4306.html和http://blog.sina.com.cn/s/blog_4673e6030101c7o4.html

     

    一些疑问

    对于MySQL的XA支持,如果在XA START/END之间的SQL语句挂了,作为RM的MySQL如何通知TM?提供的方法里只有XA PREPARE,在我的理解里,这个是用于表示第一阶段的提交成功了,于是TM就可以通过查看XA RECOVER看到PREPARE状态了。那么如何表示失败呢?超时总不太靠谱吧?

     

     

    参考

    http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_xa.htm#g1017655

    http://docs.oracle.com/cd/B10500_01/java.920/a96654/xadistra.htm

    http://liuhaodba.blog.51cto.com/6379925/1117647

    http://www.iteedu.com/database/mysql/mysqlmanualcn/sql-syntax/xa.php

    http://jishu.zol.com.cn/4306.html

    http://blog.sina.com.cn/s/blog_4673e6030101c7o4.html

    http://blog.csdn.net/mchdba/article/details/13076803

  • 相关阅读:
    html css div img垂直居中
    jquery 多选框 checkbox 获取选中的框
    css 滚动条样式
    css 翻牌 翻转 3d翻转 特效
    css强制不换行 多出的字省略号
    jquery获取元素坐标获取鼠标坐标
    鸡汤 咯咯
    <bean> 中配置详解 </bean>
    正则表达式的囧
    我的天$删除注册表$安装mysql最后一步不能启动服务的解决办法
  • 原文地址:https://www.cnblogs.com/aicro/p/3532302.html
Copyright © 2011-2022 走看看