如何设计全局分布式事务(无数据库)? JTA可以用于无数据库事务吗? [英] How to design global distributed transaction(none database)? Can JTA use for none db transaction?

查看:128
本文介绍了如何设计全局分布式事务(无数据库)? JTA可以用于无数据库事务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个相当普遍的问题:如何将我的业务逻辑放在分布式系统环境中的全局事务中?举个例子,我有一个包含几个子任务的TaskA:

I think this is a fairly common question: how to put my business logic in a global transaction in distributed systems environment? Cite an example, I have a TaskA containing couples of sub tasks:

TaskA {subtask1,subtask2,subtask3 ...}

TaskA {subtask1, subtask2, subtask3 ... }

这些子任务中的每一个都可以在本地机器或远程机器上执行,我希望TaskA通过事务以原子方式(成功或失败)执行。每个子任务都有一个回滚函数,一旦TaskA认为操作失败(因为其中一个子任务失败),它就会调用子任务的每个回滚函数。否则,TaskA会提交整个事务。

each of these subtasks may execute on local machine or a remote one, I hope TaskA executes in an atomic manner(success or fail) by means of transaction. Every subtask has a rollback function, once TaskA thinks the operation fails(because one of subtask fails), it calls each rollback function of subtasks. Otherwise TaskA commits the whole transaction.

为此,我按照审计试用事务模式来记录每个子任务,因此TaskA可以知道子任务的操作结果然后决定回滚或提交。这听起来很简单,但是,困难的部分是如何将每个子任务与全局事务相关联?

To do this, I follow "Audit trial" transaction pattern to have a record for each subtask, so TaskA can know operation results of subtasks then decide rollback or commit. This sounds like simple, however, the hard part is how to associate each subtask to the global transaction?

当TaskA开始时,它启动一个关于哪个子任务什么都不知道的全局事务。为了使子任务意识到它,我必须将事务上下文传递给每个子任务调用。这真是太可怕了!我的子任务可以在新线程中执行,也可以通过AMQP代理发送消息在远程执行,很难巩固上下文传播的方式。

When TaskA begins, it starts a global transaction about which subtask knows nothing. To make subtask aware of it, I have to pass the transaction context to every invocation of subtask. This is really dreadful! My subtask may either execute in a new thread or execute in remote by sending a message through AMQP broker, it's hard to consolidate the way of context propagation.

我做了一些研究,比如交易模式 - 四种交易相关模式的集合,异步消息传递环境中的已检查交易,这些都没有解决我的问题。他们要么没有实际的例子,要么没有解决上下文传播问题。

I did some research like "Transaction Patterns - A Collection of Four Transaction Related Patterns", "Checked Transactions in an Asynchronous Message Passing Environment", none of these solve my problem. They either don't have practical example or don't solve the context propagation issue.

我想知道人们如何解决这个问题?因为这种交易必须在企业软件中很常见。

I wonder how people solve this? as this sort of transaction must be common in enterprise software.

X / Open XA只是解决方案吗? JTA可以在这里提供帮助(我没有关注JTA,因为它与数据库事务有关,我使用Spring,我不想在我的软件中涉及另一个Java EE应用服务器)。

Is X/Open XA only the solution for this? Can JTA help here(I have't look into JTA as most stuff for it relate to database transaction, and I am using Spring, I don't want to involve another Java EE application server in my software).

有些专家可以与我分享一些想法吗?谢谢。

Can some expert share some thoughts with me? thank you.

Arjan和Martin给出了非常好的答案,谢谢。
最后我没有采用这种方式。
经过更多研究,我选择了另一种模式 CheckPoint 1

Arjan and Martin gave out really good answers, thank you. Finally I didn't go with this way. After more research, I chose another pattern "CheckPoint" 1.

查看我的要求,我发现我的审核试验事务模式的意图是知道操作已经进行到哪个级别,如果它失败了,我可以在重新加载一些上下文后在失败的地点重新启动它。实际上这不是事务,它在失败后没有回滚其他成功的步骤。这是CheckPoint模式的本质。
然而,研究分布式交易的东西让我学到了很多有趣的东西。除了Arjan和Martin提到的。我还建议人们深入研究这个领域,看看CORBA这是一个众所周知的分布式系统协议。

Looking into my requirement, I found my intention to "Audit Trial Transaction Pattern" is to know which level the operation has proceeded to, if it's failed, I can restart it at the failed spot after reloading some context. Actually this is not transaction, it didn't rollback other successful steps after failure. This is essence of CheckPoint pattern. However, studying distributed transaction stuff makes me learned lot of interesting things. Apart from what Arjan and Martin have mentioned. I also suggest people digging into this area take a look at CORBA which is a well-known protocol for distributed system.

推荐答案

你是的,由于JTA API提供的XA事务管理器,您需要两阶段提交支持。

You're right, you need two-phase commit support thanks to a XA transaction manager provided by the JTA API.

据我所知Spring没有提供事务管理器实施本身。 JtaTransactionManager 通常仅委托现有实施从JavaEE实现提供。

As far as I know Spring does not provide a transaction manager implementation itself. The JtaTransactionManager only delegates to existing implementation usually provided from JavaEE implementations.

因此,您必须将JTA实现插入Spring才能完成有效的工作。以下是一些建议:

So you will have to plugin a JTA implementation into Spring to get effective job done. Here are some proposals:

  • JOTM
  • JBossTS based on Arjuna.
  • Atokimos

然后,您必须实现资源管理器以支持两阶段提交。在JavaEE世界中,它包含一个打包为RAR存档的资源适配器。根据资源的类型,以下几个方面是阅读/实现:

Then you will have to implement your resource manager to support two-phase commit. In the JavaEE worlds, it consists in a resource adapter packaged as a RAR archive. Depending on the type of resource, the following aspects are to read/implement:

  • XAResource interface
  • JCA Java Connector Architecture mainly if a remote connection is involved
  • JTS Transaction Service if transaction propagation between nodes is required

作为示例,我建议您查看经典的带文件交易问题的实现:

As examples, I recommend you to look at implementations for the classical "transactions with files" issue:

  • the transactional file manager from JBoss Transactions
  • XADisk project

这篇关于如何设计全局分布式事务(无数据库)? JTA可以用于无数据库事务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆