Spring cloud distributed transaction Atomikos instance

Keywords: PHP Java JDBC Spring Database

Introduction to 0.JTA(Java Transaction Manager)

(1) jta and jdbc

Simply put, jta is a multi-library transaction. jdbc is a single-library transaction.

(2) XA and JTA

XA: XA is a specification or a transaction protocol. The XA protocol was first proposed by Tuxedo and handed over to the X/Open organization as the interface standard between resource manager (database) and transaction manager.

The XA specification defines:
1. Transaction Manager: This Transaction Manager can manage multiple Resouce s by managing multiple Resource Managers, that is, multiple data sources.
2. XAResource: An interface for data resource encapsulation
3. Two-stage submission: a mechanism for multi-source transaction submission

JTA(Java Transaction Manager): It's the Java specification and the implementation of XA in Java.
1. Transaction Manager: Common methods to open, roll back, and get transactions. begin(),rollback()...
2. XAResouce: Resource management, transaction management through Session, commit(xid)...
3. XID: Each transaction is assigned a specific XID

How does JTA implement transaction management for multiple data sources?

The main principle is two-stage submission. As an example, when the whole business is completed, it is only the first stage submission. Before the second stage submission, it checks whether all other transactions have been submitted. If there is an error or no submission, then the second stage will not submit, but rollback operation directly. In this way, all transactions will do the Rollback operation.

(3) jta characteristics

The point of JTA is that it can support multi-database transaction management at the same time and satisfy the consistency of data in distributed system. But it also has corresponding drawbacks:

  1. Two-stage submission
  2. Transaction time is too long, lock data too long
  3. Low performance, low throughput

1. Add spring-boot-starter-jta-atomikos to maven's pom

2. Configure multiple data sources of jta and atomikos, such as:

  jta:
    enabled: true
    atomikos:
      datasource:
        order:
          xa-properties.url: jdbc:h2:mem:dborder
          xa-properties.user: sa
          xa-properties.password:
          xa-data-source-class-name: org.h2.jdbcx.JdbcDataSource
          unique-resource-name: order
          max-pool-size: 10
          min-pool-size: 1
          max-lifetime: 10000
          borrow-connection-timeout: 10000
        log:
          xa-properties.url: jdbc:h2:mem:dblog
          xa-properties.user: sa
          xa-properties.password:
          xa-data-source-class-name: org.h2.jdbcx.JdbcDataSource
          unique-resource-name: log
          max-pool-size: 10
          min-pool-size: 1
          max-lifetime: 10000
          borrow-connection-timeout: 10000

3. Configuration of jta and jpa of atomikos and configuration of data source

import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

/**
 * Created by caibosi on 2018-07-25.
 */
public class AtomikosJtaPlatform extends AbstractJtaPlatform {

    private static TransactionManager transactionManager;

    private static UserTransaction userTransaction;

    public static void setTransactionManager(TransactionManager transactionManager) {
        AtomikosJtaPlatform.transactionManager = transactionManager;
    }

    public static void setUserTransaction(UserTransaction userTransaction) {
        AtomikosJtaPlatform.userTransaction = userTransaction;
    }

    @Override
    protected TransactionManager locateTransactionManager() {
        return transactionManager;
    }

    @Override
    protected UserTransaction locateUserTransaction() {
        return userTransaction;
    }
}

4. Use jta to use @Transactional on Methods

    @Transactional
    public void newOrderRollback(String userId,String productCode,int quantity){
        UserOrder userOrder = new UserOrder();
        userOrder.setUserId(userId);
        userOrder.setProductCode(productCode);
        userOrder.setQuantity(quantity);
        userOrderDao.save(userOrder);

        EventLog eventLog = new EventLog();
        eventLog.setOperation("new order");
        eventLog.setOperator(userId);
        eventLogDao.save(eventLog);

        throw new RuntimeException("test jta rollback");
    }

 

5. Rollback when calling newOrderRollback

Reference: https://github.com/Spring Cloud/spring-cloud-code.git ch24

  https://www.jianshu.com/p/3938e7172443

Posted by alext on Sat, 20 Jul 2019 02:44:14 -0700