Distributed transactions in Spring, with and without XA

While it‘s common to use the Java Transaction API and the XA protocol for distributed transactions in Spring, you do have other options. The optimum implementation depends on the types of resources your application uses and the trade-offs you‘re willing to make between performance, safety, reliability, and data integrity. In this JavaWorld feature, SpringSource‘s David Syer guides you through seven patterns for distributed transactions in Spring applications, three of them with XA and four without. Level: Intermediate

The Spring Framework‘s support for the Java Transaction API (JTA) enables applications to use distributed transactions and the XA protocol without running in a Java EE container. Even with this support, however, XA is expensive and can be unreliable or cumbersome to administrate. It may come as a welcome surprise, then, that a certain class of applications can avoid the use of XA altogether.

To help you understand the considerations involved in various approaches to distributed transactions, I‘ll analyze seven transaction-processing patterns, providing code samples to make them concrete. I‘ll present the patterns in reverse order of safety or reliability, starting with those with the highest guarantee of data integrity and atomicity under the most general circumstances. As you move down the list, more caveats and limitations will apply. The patterns are also roughly in reverse order of runtime cost (starting with the most expensive). The patterns are all architectural, or technical, as opposed to business patterns, so I don‘t focus on the business use case, only on the minimal amount of code to see each pattern working.

Note that only the first three patterns involve XA, and those might not be available or acceptable on performance grounds. I don‘t discuss the XA patterns as extensively as the others because they are covered elsewhere, though I do provide a simple demonstration of the first one. By reading this article you‘ll learn what you can and can‘t do with distributed transactions and how and when to avoid the use of XA -- and when not to.

Distributed transactions and atomicity

A distributed transaction is one that involves more than one transactional resource. Examples of transactional resources are the connectors for communicating with relational databases and messaging middleware. Often such a resource has an API that looks something like begin(), rollback(), commit(). In the Java world, a transactional resource usually shows up as the product of a factory provided by the underlying platform: for a database, it‘s a Connection (produced by DataSource) or Java Persistence API (JPA) EntityManager; for Java Message Service (JMS), it‘s a Session.

In a typical example, a JMS message triggers a database update. Broken down into a timeline, a successful interaction goes something like this:

  1. Start messaging transaction
  2. Receive message
  3. Start database transaction
  4. Update database
  5. Commit database transaction
  6. Commit messaging transaction

If a database error such as a constraint violation occurred on the update, the desirable sequence would look like this:

[ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-part course! ]

  1. Start messaging transaction
  2. Receive message
  3. Start database transaction
  4. Update database, fail!
  5. Roll back database transaction
  6. Roll back messaging transaction

In this case, the message goes back to the middleware after the last rollback and returns at some point to be received in another transaction. This is usually a good thing, because otherwise you might have no record that a failure occurred. (Mechanisms to deal with automatic retry and handling exceptions are out of this article‘s scope.)

The important feature of both timelines is that they are atomic, forming a single logical transaction that either succeeds completely or fails completely.

But what guarantees that the timeline looks like either of these sequences? Some synchronization between the transactional resources must occur, so that if one commits they both do, and vice versa. Otherwise, the whole transaction is not atomic. The transaction is distributed because multiple resources are involved, and without synchronization it will not be atomic. The technical and conceptual difficulties with distributed transactions all relate to the synchronization of the resources (or lack of it).

The first three patterns discussed below are based on the XA protocol. Because these patterns have been widely covered, I won‘t go into much detail about them here. Those familiar with XA patterns may want to skip ahead to the Shared Transaction Resource pattern.

Full XA with 2PC

If you need close-to-bulletproof guarantees that your application‘s transactions will recover after an outage, including a server crash, then Full XA is your only choice. The shared resource that is used to synchronize the transaction in this case is a special transaction manager that coordinates information about the process using the XA protocol. In Java, from the developer‘s point of view, the protocol is exposed through a JTA UserTransaction.

Being a system interface, XA is an enabling technology that most developers never see. They need to know is that it‘s there, what it enables, what it costs, and the implications for how they use transactional resources. The cost comes from the two-phase commit (2PC) protocol that the transaction manager uses to ensure that all resources agree on the outcome of a transaction before it ends.

If the application is Spring-enabled, it uses the Spring JtaTransactionManager and Spring declarative transaction management to hide the details of the underlying synchronization. The difference for the developer between using XA and not using XA is all about configuring the factory resources: the DataSource instances, and the transaction manager for the application. This article includes a sample application (the atomikos-db project) that illustrates this configuration. The DataSource instances and the transaction manager are the only XA- or JTA-specific elements of the application.

To see the sample working, run the unit tests under com.springsource.open.db. A simple MulipleDataSourceTests class just inserts data into two data sources and then uses the Spring integration support features to roll back the transaction, as shown in Listing 1:

Listing 1. Transaction rollback



  @Transactional
  @Test
  public void testInsertIntoTwoDataSources() throws Exception {

    int count = getJdbcTemplate().update(
        "INSERT into T_FOOS (id,name,foo_date) values (?,?,null)", 0,
        "foo");
    assertEquals(1, count);

    count = getOtherJdbcTemplate()
        .update(
            "INSERT into T_AUDITS (id,operation,name,audit_date) values (?,?,?,?)",
            0, "INSERT", "foo", new Date());
    assertEquals(1, count);

    // Changes will roll back after this method exits

  }

Then MulipleDataSourceTests verifies that the two operations were both rolled back, as shown in Listing 2:

Listing 2. Verifying rollback

 @AfterTransaction
  public void checkPostConditions() {

    int count = getJdbcTemplate().queryForInt("select count(*) from T_FOOS");
    // This change was rolled back by the test framework
    assertEquals(0, count);

    count = getOtherJdbcTemplate().queryForInt("select count(*) from T_AUDITS");
    // This rolled back as well because of the XA
    assertEquals(0, count);

  }

For a better understanding of how Spring transaction management works and how to configure it generally, see the Spring Reference Guide.

XA with 1PC Optimization

This pattern is an optimization that many transaction managers use to avoid the overhead of 2PC if the transaction includes a single resource. You would expect your application server to be able to figure this out.

XA and the Last Resource Gambit

Another feature of many XA transaction managers is that they can still provide the same recovery guarantees when all but one resource is XA-capable as they can when they all are. They do this by ordering the resources and using the non-XA resource as a casting vote. If it fails to commit, then all the other resources can be rolled back. It is close to 100 percent bulletproof -- but is not quite that. And when it fails, it fails without leaving much of a trace unless extra steps are taken (as is done in some of the top-end implementations).

Shared Transaction Resource pattern

A great pattern for decreasing complexity and increasing throughput in some systems is to remove the need for XA altogether by ensuring that all the transactional resources in the system are actually backed by the same resource. This is clearly not possible in all processing use cases, but it is just as solid as XA and usually much faster. The Shared Transaction Resource pattern is bulletproof but specific to certain platforms and processing scenarios.

A simple and familiar (to many) example of this pattern is the sharing of a database Connection between a component that uses object-relational mapping (ORM) with a component that uses JDBC. This is what happens you use the Spring transaction managers that support the ORM tools such as Hibernate, EclipseLink, and the Java Persistence API (JPA). The same transaction can safely be used across ORM and JDBC components, usually driven from above by a service-level method execution where the transaction is controlled.

Another effective use of this pattern is the case of message-driven update of a single database (as in the simple example in this article‘s introduction). Messaging-middleware systems need to store their data somewhere, often in a relational database. To implement this pattern, all that‘s needed is to point the messaging system at the same database the business data is going into. This pattern relies on the messaging-middleware vendor exposing the details of its storage strategy so that it can be configured to point to the same database and hook into the same transaction.

Not all vendors make this easy. An alternative, which works for almost any database, is to use Apache ActiveMQ for messaging and plug a storage strategy into the message broker. This is fairly easy to configure once you know the trick. It‘s demonstrated in this article‘s shared-jms-db samples project. The application code (unit tests in this case) does not need to be aware that this pattern is in use, because it is all enabled declaratively in Spring configuration.

A unit test in the sample called SynchronousMessageTriggerAndRollbackTests verifies that everything is working with synchronous message reception. The testReceiveMessageUpdateDatabase method receives two messages and uses them to insert two records in the database. When this method exits, the test framework rolls back the transaction, so you can verify that the messages and the database updates are both rolled back, as shown in Listing 3:

Listing 3. Verifying rollback of messages and database updates

@AfterTransaction
public void checkPostConditions() {

  assertEquals(0, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
  List<String> list = getMessages();
  assertEquals(2, list.size());

}

The most important features of the configuration are the ActiveMQ persistence strategy, linking the messaging system to the same DataSource as the business data, and the flag on the Spring JmsTemplate used to receive the messages. Listing 4 shows how to configure the ActiveMQ persistence strategy:

Listing 4. Configuring ActiveMQ persistence

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
  depends-on="brokerService">
    <property name="brokerURL" value="vm://localhost?async=false" />
</bean>
<bean id="brokerService" class="org.apache.activemq.broker.BrokerService" init-method="start"
  destroy-method="stop">
    ...

    <property name="persistenceAdapter">
        <bean class="org.apache.activemq.store.jdbc.JDBCPersistenceAdapter">
            <property name="dataSource">
                <bean class="com.springsource.open.jms.JmsTransactionAwareDataSourceProxy">
                    <property name="targetDataSource" ref="dataSource"/>
                    <property name="jmsTemplate" ref="jmsTemplate"/>
                </bean>
            </property>
            <property name="createTablesOnStartup" value="true" />
        </bean>
    </property>
</bean>

Listing 5 shows the flag on the Spring JmsTemplate that is used to receive the messages:

Listing 5. Setting up the JmsTemplate for transactional use

<beanid="jmsTemplate"class="org.springframework.jms.core.JmsTemplate">
      ...

    <!-- This is important... -->
    <propertyname="sessionTransacted"value="true"/>
</bean>

Without sessionTransacted=true, the JMS session transaction API calls will never be made and the message reception cannot be rolled back. The important ingredients here are the embedded broker with a special async=false parameter and a wrapper for the DataSource that together ensure that ActiveMQ uses the same transactional JDBC Connection as Spring.

#drr-container figure.large.video { border-bottom: 0 none }
figure#page-lede.thm-gallery.rr-gallery-vid #bcplayer-gallery #bcplayer-gallery_ad>div { width: 300px !important; height: 169px !important }
figure#page-lede.thm-gallery #bcplayer-gallery #bcplayer-gallery_ad>div { width: 100%; height: 100% }
.jwplayer.jw-state-paused .jw-display { display: table !important; padding: 0 }
.jwplayer .jw-display-icon-container { float: none; padding: 0; margin: 0 }
.jw-flag-small-player .jw-display { padding-top: 0px }
.jwplayer .jw-display-icon-container .jw-icon-rewind { visibility: hidden }
.jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-play path,.jw-state-idle .jw-svg-icon-play path,.jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-replay path,.jw-state-complete .jw-svg-icon-replay path { display: none }
.jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-play,.jw-state-idle .jw-svg-icon-play,.jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-replay,.jw-state-complete .jw-svg-icon-replay { background-repeat: no-repeat; background-color: transparent; background-image: url("//idge.staticworld.net/idgtv/btn-play-default.svg"); background-position: center center; bottom: 0; left: 0; margin: auto; right: 0; top: 0 }
.jwplayer .jw-display-icon-container .jw-icon,.jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-play,.jw-state-idle .jw-svg-icon-play,.jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-replay,.jw-state-complete .jw-svg-icon-replay { height: 81px; width: 78px }
.thm-gallery.rr-gallery-vid .jwplayer .jw-display-icon-container .jw-icon,.thm-gallery.rr-gallery-vid .jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-play,.thm-gallery.rr-gallery-vid .jw-state-idle .jw-svg-icon-play,.thm-gallery.rr-gallery-vid .jwplayer .jw-display-icon-container .jw-icon-display .jw-svg-icon-replay,.thm-gallery.rr-gallery-vid .jw-state-complete .jw-svg-icon-replay { height: 65px; width: 62px }

原文地址:https://www.cnblogs.com/kaleidoscope/p/9766864.html

时间: 2024-10-11 13:03:20

Distributed transactions in Spring, with and without XA的相关文章

Distributed traceability with Spring Cloud: Sleuth and Zipkin

I. Sleuth 0. Concept Trace A set of spans that form a call tree structure, forms the trace of the request. Span It is the basic unit of work, for example a call to a service. They are identified with a span ID and a trace ID to which span is owned. T

Spring with and without XA

http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html 文章涉及的代码: 1. <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">

spring Transaction Management --官方

原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12. Transaction Management 12.1 Introduction to Spring Framework transaction management Comprehensive transaction support is among the most compelling rea

数据库分库分表(sharding)系列

数据库分库分表(sharding)系列     目录; (一) 拆分实施策略和示例演示 (二) 全局主键生成策略 (三) 关于使用框架还是自主开发以及sharding实现层面的考量 (四) 多数据源的事务处理 (五) 一种支持自由规划无须数据迁移和修改路由代码的Sharding扩容方案 (一) 拆分实施策略和示例演示 第一部分:实施策略 图1.数据库分库分表(sharding)实施策略图解 1.准备阶段 对数据库进行分库分表(Sharding化)前,需要开发人员充分了解系统业务逻辑和数据库sch

数据库分库分表(sharding)系列(四) 多数据源的事务处理

系统经sharding改造之后,原来单一的数据库会演变成多个数据库,如何确保多数据源同时操作的原子性和一致性是不得不考虑的一个问题.总体上看,目前对于一个分布式系统的事务处理有三种方式:分布式事务.基于Best Efforts 1PC模式的事务以及事务补偿机制.我们下面对这三种处理方式一一进行分析.本文原文链接:http://blog.csdn.net/bluishglc/article/details/7793172 转载请注明出处! 分布式事务 这是最为人们所熟知的多数据源事务处理机制.本文

Spring JTA multiple resource transactions in Tomcat with Atomikos example--转载

原文地址:http://www.javacodegeeks.com/2013/07/spring-jta-multiple-resource-transactions-in-tomcat-with-atomikos-example.html In this tutorial we shall show you how to implement JTA multiple resource transactions in a Tomcat server, using Atomikos Transac

spring分布式事务学习笔记

最近项目中使用了分布式事务,本文及接下来两篇文章总结一下在项目中学到的知识. 分布式事务对性能有一定的影响,所以不是最佳的解决方案,能通过设计避免最好尽量避免. 分布式事务(Distributed transactions),也称作XA事务(XA是一个协议的名字),在spring中被称作global transaction,是指一个事务会涉及到不同的事务资源,比如不同的数据库,消息队列.事务资源都支持commit和rollback这样的事务控制命令. 按是否需要实现完整javaEE功能的应用服务

分布式系统(Distributed System)资料

这个资料关于分布式系统资料,作者写的太好了.拿过来以备用 网址:https://github.com/ty4z2008/Qix/blob/master/ds.md 希望转载的朋友,你可以不用联系我.但是一定要保留原文链接,因为这个项目还在继续也在不定期更新.希望看到文章的朋友能够学到更多. <Reconfigurable Distributed Storage for Dynamic Networks> 介绍:这是一篇介绍在动态网络里面实现分布式系统重构的paper.论文的作者(导师)是MIT

MySQL binlog 组提交与 XA(两阶段提交)

1. XA-2PC (two phase commit, 两阶段提交 ) XA是由X/Open组织提出的分布式事务的规范(X代表transaction; A代表accordant?).XA规范主要定义了(全局)事务管理器(TM: Transaction Manager)和(局部)资源管理器(RM: Resource Manager)之间的接口.XA为了实现分布式事务,将事务的提交分成了两个阶段:也就是2PC (tow phase commit),XA协议就是通过将事务的提交分为两个阶段来实现分布