(参考并转载自http://blog.donews.com/Ralph/archive/2004/11/18/174646.aspx)

关于分布式事务,我觉得有必要了解清楚以下几点:

(1)目前流行的分布式事务协议DTP,

(2)Java如何定义实现规范

(3)常用的第三方JTA以及使用配置方法

1.分布式事务的协议:DTP 和 Two - Phase Commit Protocol

在分布式事务管理领域,X/Open的DTP模型是业界最被广泛接受分布式事务管理模型。几乎所有的事务处理产品,关系数据库以及消息产品都支持该DTP模型中定义的接口。下面是对DTP模型的一段描述:

X/Open’s

DTP model defines three components: application programs, resource

managers(such as RDBMS), and a transaction manager. This model also

specifies functional interfaces between application programs and the

transaction manager (known as the TX interface), and between the

transaction manager and the resource managers (the XA interface). With

products complying to these interfaces, one can implement transactions

with the two phase commit and recovery protocol to preserve atomicity

of transactions.

DTP 模型中有一个非常重要的模型,叫Two - Phase Commit

Protocol。该协议作用于transaction manager和resource managers之间,保证所有的resource

managers要么都commit, 要么都abort。

具体的TPC protocol执行过程如下:

The first

phase of this protocol is the preparation phase, during which the

transaction manager conducts a voting among all the resource managers

registered for the target transaction. The transaction manager calls

the prepare method on each resource manager, which will return a

X_READONLY or XA_OK constant. The first value implies that the

operations conducted on the target resource are read only, and there

are no operations to be committed. The second value indicates that the

resource manager is ready to commit the operations. In case the

resource manager wants the transaction to be rolled back, it throws an

appropriate XAException.

The second phase is a commit or recover

phase. Depending on whether all resource managers are ready for a

commit or not, one of these phases will be invoked by the transaction

manager. In case all the resource managers return a X_OK in the first

phase, the transaction manager calls the commit method on each resource

manager. Please note that the two phase protocol is not fool-proof, and

the commit calls may throw appropriate XAException in return. In such a

case, the transaction manager should initiate the recovery phase. In

case the voting determines a rollback, the transaction calls the

recover on each prepared resource manager.

2. Java Transaction Service

sun并未提供Java Persistence API的实现,只是定义了一套接口和规范。Java的事务实现构架遵循了DTP模型,主要有两部分组成:JTS(Java Transaction Service)和JTA(Java Transaction API)。JTS主要规定了DTP模型中transaction manager的实现方式,而JTA则定义了application programs, transaction manager及resource manager之间的接口。

JTA可以分为三类, 分别为针对application接口,transaction manager接口和resource manager接口。

(1)application接口包括:

javax.transaction.Synchronization:An

object intended to participate in a synchronization protocol with the

transaction manager should implement this interface. This mechanism is

based on the Observer pattern. This interface has two methods -

beforeCompletion and afterCompletion to be called before starting and

after completing, respectively, the two phase commit operation.

(2)transaction manager接口:

javax.transaction.Status:

Defines the flags for the status of a transaction. The

javax.transaction.Transaction, javax.transaction.TransactionManager,

and javax.transaction.UserTransaction interfaces provide a getStatus

method that returns one of the above status flags.

javax.transaction.Transaction:

An object of this type is created for each global transaction. This

interface provides methods for transaction completion(commit and

rollback), resource enlistment (enlistResource) and delistment

(delistResource), registration of synchronization objects

(registerSynchronization), and query of status of the transaction

(getStatus).

javax.transaction.TransactionManager: This

interface is implemented by the JTS and allows an application server to

communicate with the transaction manager to demarcate transactions

(begin, commit, rollback, suspend and resume), set the transaction for

rollback (setRollbackOnly), get the associated Transaction object

(getTransaction), set the transaction timeout interval

(setTransactionTimeout) and query the status of the transaction

(getStatus).

javax.transaction.UserTransaction: This interface

allows application components to manage transaction boundaries

explicitly. This interface provides methods to begin and end

transactions (begin, commit, and rollback), set the transaction for

rollback (setRollbackOnly), set the transaction timeout interval

(setTransactionTimeout), and get the status of the transaction

(getStatus). The application server should provide an interface to

create an object of this type.

javax.transaction.xa.Xid: This

interface is a Java mapping of the X/Open transaction identifier xid

structure. The transaction manager uses an object of this type to

associate a resource manager with a transaction.

(3)resource manager接口:

javax.transaction.xa.XAResource:

This is a Java mapping of the X/Open XA interface, and is implemented

by resource managers operating with the JTS. This interface provides

methods to start (start) and end (end) work on behalf of a specified

transaction, to prepare a transaction with the current resource

(prepare), to end transactions with the current resource (commit,

forget, recover, and rollback), to compare the current resource manager

with another resource manager (isSameRM), and to get and set the

transaction timeout (getTransactionTimeout, setTransactionTimeout).

使用JTA进行分布式事务编程有两种方法,一种是通过javax.transaction.UserTransaction,另一种是通过javax.transaction.TransactionManager。但这两种方法之间的区别不是很清楚?

运用javax.transaction.UserTransaction(以Weblogic6.1为例):

//get UserTransaction

//in websphere, jndi name is “java:comp/UserTransaction”

UserTransaction  userTrans =   (UserTransaction)getInitialContext().lookup("javax.transaction.UserTransaction");

//begin transaction

userTrans.begin();

//transactional operations

//end transaction

userTrans.commit();

运用javax.transaction.TransactionManager(以Weblogic6.1为例):

//get UserTransaction

UserTransaction  transManager =   (UserTransaction)getInitialContext().lookup("javax.transaction.TransactionManager");

//begin transaction

Transaction trans = transManager.begin();

//transactional operations

//end transaction

transManager.commit();

Transaction

对象封装了事务上下文,通过该对象的enlistResource(),delistResource()方法可以添加删除参与事务的resource

manager, 通过registerSynchronization()方法可以注册监听对象。

3.AtomikosTransactionsEssentials简介

关于这一点可以参考:http://momoko8443.javaeye.com/blog/190994

java jta事务_JTA -- Java分布式事务管理相关推荐

  1. Java jta 原理_Java的分布式事务(JTA和XA)

    Java的分布式事务(JTA和XA) Java的分布式事务(JTA和XA) 关于JTA事务和XA事务的介绍 何为分布式事务 一个事务包含多个操作,多个操作操作了多个数据源,这样的事务称为分布式事务. ...

  2. java分布式事务原理_JAVA分布式事务原理及应用

    JTA(Java Transaction API)允许应用程序执行分布式事务处理--在两个或多个网络计算机资源上访问并且更新数据.JDBC驱动程序的JTA支持极大地增强了数据访问能力. 本文的目的是要 ...

  3. jta mysql_JTA 使用 MySQL 分布式事务

    假定在MySQL实例1上有表 create table person( id int, name varchar(32) ) MySQL实例2上也有一张同样的表,现在从实例1中的 person 表中删 ...

  4. spring boot+Mybatis+mysql+atomikos+jta实现多数据源分布式事务

    spring boot+Mybatis+mysql+atomikos+jta实现多数据源分布式事务 1.导入相关依赖 2.配置相关application.properties 3.创建配置文件 4.创 ...

  5. 事务模型与分布式事务总结思考

    转载自 事务模型与分布式事务总结思考 1. 介绍 之前了解过一些分布式事务处理的思想,包括MVCC.TCC等.但是对具体实现的规范和约束还不够理解清晰.本文从事务模型分类来讨论常见的事务模型.事务模型 ...

  6. 分布式事务讲解 - TX-LCN分布式事务框架(含LCN、TCC、TXC三种模式)

    分布式事务讲解 - TX-LCN分布式事务框架(含LCN.TCC.TXC三种模式) 分布式事务系列博客: TX-LCN框架原理 LCN 原理及主要特点 代码实现 实现场景 创建数据库及表(三个数据库, ...

  7. 分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ

    分布式事务--分布式事务简介.分布式事务框架 Seata(AT模式.Tcc模式.Tcc Vs AT).分布式事务--MQ 一.分布式事务简介 如果不是分布式环境的话一般不会接触到这种,一旦是微服务这种 ...

  8. 【重难点】【事务 03】分布式事务

    [重难点][事务 03]分布式事务 文章目录 [重难点][事务 03]分布式事务 一.为什么需要分布式系统架构 二.系统架构演进 1.单体应用架构 2.垂直应用架构 3.分布式架构 4.SOA 架构 ...

  9. 分布式事务基础(分布式事务协议解决方案)

    文章目录 分布式事务基础 1. 基本概念 1.1. 什么是事务? 1.2. 事务的四个特性(ACID) 1.3. 什么是分布式事务 ? 1.4. 分布式事务参数场景 1.4.1. 跨JVM进程产生分布 ...

  10. 搞懂分布式技术19:使用RocketMQ事务消息解决分布式事务

    搞懂分布式技术19:使用RocketMQ事务消息解决分布式事务 初步认识RocketMQ的核心模块 rocketmq模块 rocketmq-broker:接受生产者发来的消息并存储(通过调用rocke ...

最新文章

  1. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM
  2. 常用的HTTP状态码
  3. 像程序员一样思考:如何仅使用JavaScript,HTML和CSS来构建Snake
  4. CVPR 2020丨基于范例的精细可控图像翻译CoCosNet,一键生成你心目中的图像
  5. 学hadoop需要什么基础
  6. python基础教程免费下载-《Python机器学习基础教程》高清版免费PDF下载
  7. python学了有什么用-python学来有什么用
  8. 盘点前 10 名的免费跨浏览器测试工具
  9. MySQL卸载干净检查_MySQL数据库卸载干净处理
  10. servlet生成验证码和点击刷新验证码
  11. j1900适合装哪版群晖_适合新人观看的威联通NAS设置流程详解攻略
  12. 记一次公众号开发途中的诡异事件
  13. 有什么免费的视频格式转换工具?快试试这4款,堪称“良心”工具
  14. SOA+LDAP实现SSO单点登录思路
  15. 八十八枚红手印背后的故事
  16. HDU1495 非常可乐【倒水问题+BFS】
  17. 使用python 绘制统计图,(折线图,条形统计图,扇形统计图)
  18. Windows Azure实战pdf
  19. 2.2.1 数据通信系统的模型
  20. 网上书城项目分析及前端页面

热门文章

  1. wordpress迁移
  2. CG爱好者 3dmax maya osg ---初识篇
  3. 四柱汉诺塔 -- 动态规划求解最优移动次数
  4. 数据分析中常用到EXCEL快捷键合集
  5. 【GeoscenePro应用】利用体素图层构建三维地质体模型
  6. VFW-MFC视频采集
  7. python字符串s最后一个字符的位置是_python截取字符串后几位?
  8. 人工智能数据和算法的偏差_一个AI培训工具已经将其偏差传递给了将近两个十年的算法
  9. CSS优化滚动条样式设置
  10. MATLAB 相机标定(单目)使用工具箱TOOLBOX_calib