Java 事务回滚和 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/434950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transaction rollback and web services
提问by Andreas Tasoulas
Given an example of calling two web services methods from a session bean, what if an exception is thrown between the calls to two methods? In the case of not calling the web services the transaction will rollback and no harm done. However, the web service will not rollback. Of course, even with a single web service there is a problem. While this is a generic question I am interested in solutions having to do with EJB session beans.
给出一个从会话 bean 调用两个 Web 服务方法的示例,如果在对两个方法的调用之间抛出异常怎么办?在不调用 Web 服务的情况下,事务将回滚并且不会造成任何伤害。但是,Web 服务不会回滚。当然,即使使用单个 Web 服务也存在问题。虽然这是一个通用问题,但我对与 EJB 会话 bean 相关的解决方案感兴趣。
An easy and customized answer would be to add a special "rollback method" to the web service for each "real functionality" method. What I am asking for is some standardized way to do so.
一个简单且定制的答案是为每个“真正的功能”方法向 Web 服务添加一个特殊的“回滚方法”。我要求的是一些标准化的方法来做到这一点。
采纳答案by Andreas Tasoulas
A number of techniques are evolving, but the problem is still sufficiently cutting edge that the standardization process has not yet provided us with a totally portable solution.
许多技术正在发展,但问题仍然足够前沿,标准化过程尚未为我们提供完全可移植的解决方案。
Option one, you can make the web services transaction aware. This of course assumes you have control over them, although writing a transaction aware proxy for non-transactional services is also an option in some cases.
选项一,您可以使 Web 服务事务感知。这当然假设您可以控制它们,尽管在某些情况下为非事务性服务编写事务感知代理也是一种选择。
The WS-AT and WS-BA protocols are the leading standards for transactional web services. Unfortunately they specify the protocol only, not the language bindings. In other words, there is no standard API at the programming language level. For Java the nearest thing is JSR-156, but it's not ready yet.
WS-AT 和 WS-BA 协议是事务性 Web 服务的领先标准。不幸的是,他们只指定了协议,而不是语言绑定。换句话说,在编程语言层面没有标准的 API。对于 Java,最接近的是 JSR-156,但它还没有准备好。
Then the problem becomes: how to tie the EJB (i.e. JTA/XA) transaction to the WS one. Since the models used by the WS-AT and XA protocols are closely related, this can be achieved by means of a protocol bridge. Several app servers provide something alone these lines. JBoss presented theirs at JavaOne - see http://anonsvn.jboss.org/repos/labs/labs/jbosstm/workspace/jhalliday/txbridge/BOF-4182.odp
那么问题就变成了:如何将 EJB(即 JTA/XA)事务与 WS 事务联系起来。由于 WS-AT 和 XA 协议使用的模型密切相关,这可以通过协议桥来实现。几个应用服务器单独提供这些行。JBoss 在 JavaOne 上展示了他们的成果 - 见http://anonsvn.jboss.org/repos/labs/labs/jbosstm/workspace/jhalliday/txbridge/BOF-4182.odp
Note that the protocol bridging technique can also be used the other way around, to allow an EJB that uses e.g. an XA database backend, to be exposed as a transactional web service.
请注意,协议桥接技术也可以反过来使用,以允许使用例如 XA 数据库后端的 EJB 作为事务性 Web 服务公开。
However, the locking model used by two phase commit transactions is really only suitable for short lived transactions in the same domain of control. If your services run in the same company datacenter you'll probably get away with it. For wider distribution, be it geographical or administrative, you probably want to look at WS-BA, a web service transactions protocol specifically designed for such use.
但是,两阶段提交事务使用的锁定模型实际上只适用于同一控制域中的短期事务。如果您的服务在同一个公司数据中心运行,您可能会侥幸逃脱。对于更广泛的分布,无论是地理分布还是管理分布,您可能需要查看 WS-BA,一种专为此类用途设计的 Web 服务事务协议。
WS-BA uses a compensation based model that is harder to program. It's essentially based on the technique you mention: the effect of service methods is undone by calling a compensation method. This can be tricky to get right, but a JBoss intern did a rather nice annotation framework that allows you to define compensation methods with minimal effort and have them driven automatically. It's not standardized, but well worth checking out if you choose this approach: http://www.jboss.org/jbosstm/baframework
WS-BA 使用更难编程的基于补偿的模型。它基本上基于您提到的技术:通过调用补偿方法来撤消服务方法的效果。这可能很难做到,但 JBoss 实习生做了一个相当不错的注释框架,它允许您以最少的努力定义补偿方法并自动驱动它们。它不是标准化的,但如果您选择这种方法,则非常值得一试:http: //www.jboss.org/jbosstm/baframework
回答by gedevan
Web Services-Coordination (WS-C) and Web Services-Transaction (WS-T) specifications developed by Microsoft, BEA Systems and IBM are used in such cases as I know. You can start from reading Web services transactionsand A comparison of Web services transaction protocolsarticles provided by IBM to make it clear.
据我所知,由 Microsoft、BEA Systems 和 IBM 开发的 Web 服务协调 (WS-C) 和 Web 服务事务 (WS-T) 规范用于此类情况。您可以从阅读IBM 提供的 Web 服务事务和Web 服务事务协议的比较文章开始,将其弄清楚。
回答by Guy Pardon
Actually, you usually don't just need a custom rollback method but also a custom commit method. Otherwise, you get into problems like those found in the WS-BA standard.
实际上,您通常不仅需要自定义回滚方法,还需要自定义提交方法。否则,您会遇到类似 WS-BA 标准中发现的问题。
Just check out http://www.atomikos.com/Publications/TryCancelConfirmfor a detailed article. The features mentioned there are available in Atomikos ExtremeTransactions... That product also supports classical 'ACID' style web service transactions.
刚退房 http://www.atomikos.com/Publications/TryCancelConfirm的详细文章。那里提到的功能在Atomikos ExtremeTransactions中可用......该产品还支持经典的“ACID”风格的 Web 服务事务。
HTH
HTH
Guy
盖伊
Disclaimer: I work for Atomikos
免责声明:我为 Atomikos 工作