Transactions in Java--Global transactions vs. local transactions

Source: Internet
Author: User

As mentioned in the previous article, there are three types of Java transactions: JDBC Transaction, JTA (Java Transaction API) transaction, container transaction.

This is differentiated from the perspective of the implementation of the transaction, and this article differentiates the transactions in Java from another perspective. From the point of view of transaction management, you can divide the transactions used in Java into local and global transactions.

Local transaction

Use the resource Manager to control transactions without using the programmatic framework of the transaction to manage transactions. Typical is the setautocommit, commit, rollback method in Java.sql.Connection. The JDBC transaction we introduced earlier is a very typical local transaction. Local transactions are also the most frequently used transactions in our daily development.

Advantages of local transactions

Support for rigorous ACID properties

Reliable

Efficient

Status can be maintained only in the resource manager

Simple application programming Model

Limitations of local transactions

No distributed transaction processing capability

The smallest unit of isolation is determined by the resource manager, such as a record in the database

Local transactions are relatively simple, and the basic principle is the transaction principle of the database. Students who are not familiar with the transaction can read other things about the transaction in my blog.

Global transaction

We described the local transactions earlier, and local transactions are the transactions that we are often exposed to in programming, such as typical JDBC operations, which are very good at ensuring acid. However, local transactions do not resolve transactional issues in distributed scenarios.

My exploration of distributed consistency specifically describes why transactions are required in a distributed scenario. Here, I'll look back a little bit.

A typical distributed transaction scenario

Transfer

Questions about transfers between bank accounts. Account A to Account B transfer, from the implementation of the general can be divided into "debit from Account A", "Add money to account B" two operation steps, two accounts will be segmented to different databases in most cases, more, two operations will be two service calls. Both of these operations require either success at the same time or failure at the same time. Therefore, a distributed transaction problem is introduced.

Next order

On the e-commerce website, after the consumer clicks the purchase button, the transaction backstage will carry on the stock check, the order, the reduction inventory, the update orders status and so on a series of service calls, each operation corresponds to a separate service, the service generally has the independent database, therefore may produce the distributed transaction question.

The operation is not guaranteed to be processed in the same transaction because the database to which the data is written is inconsistent, or if the service being invoked is an RPC service. Therefore, there is a distributed transaction problem.

Definition of a global transaction

In the above scenario there is a distributed transaction problem, then the global transaction is a standard distributed transaction. Here we try to define the next global transaction:

A global transaction is a transaction that is managed and coordinated by the resource manager.

The global transaction is a DTP model of the transaction, the so-called DTP model refers to the X/open DTP (x/open distributed Transaction processing Reference Model), is X/open This organization defines a set of standards for distributed transactions, that is, define the specification and API interface, which is implemented by this vendor.

X/open DTP defines three components: AP,TM,RM and two protocols: XA, TX

AP (Application program): This is the application that can be understood as the use of DTP programs

RM (Resource Manager): Resource Manager, which can be understood as a DBMS system, or a messaging server management system, where the application controls resources through the resource manager.

TM (Transaction Manager): Transaction manager, responsible for coordinating and managing transactions, providing to AP application programming interfaces, and managing resource managers.

XA protocol: An interface for applications or application servers to communicate before transaction management

TX protocol: Interface for communication between the global transaction manager and the resource Manager

The transaction manager controls the global transaction, manages the transaction life cycle, and coordinates the resources. The resource manager is responsible for controlling and managing actual resources.

There is also a point here, that is, 2PC (two-phase commit), in the global transaction, in order to ensure that all operations can be one-time or full commit, or all failed. Transaction manager and Resource Manager transaction operations between the control is the use of 2PC to do, about 2PC, I have articles on the blog specifically introduced, here no longer repeat.

Implementation of global transaction in Java EE

Java itself provides a number of APIs that can be used to implement global transactions. Transaction--JDBC transactions in Java and JTA transactions described in JTA transactions can be used to implement global transactions in the EE.

JTA (Java Transaction API): A high-level transactional interface for applications, application servers, and resource managers.

JTS (Java Transaction Service): JTA The implementation standard of the transaction manager, supporting JTA up, and the interoperability of cross-transactional domains down through CORBA ots.

EJB: A component-based application programming model that further simplifies the programming of transactional applications through declarative transaction management.

Advantages and disadvantages of global transactions

Global transaction, as a standard distributed transaction solution, solves the requirement that local transactions cannot meet the acid requirements of data in distributed scenarios.

In the distributed transaction, the two-phase commit protocol, the Sankai submission protocol, I have introduced, 2PC itself is a synchronous blocking problem, which will lead to inefficient, so, the use of 2PC transaction control of the global transaction will inevitably have a low efficiency problem. This is also the most deadly drawback of global affairs, which is intolerable in the promotion of micro-services today.

Summarize

This article mainly introduces the local and global transactions, the local transaction is very simple, in Java can use JDBC to implement local transactions, global transaction is a basic distributed transaction solution, is compliant with the DTP model transaction management mechanism.

At present, more and more web development involves the distributed transaction, especially the micro-service architecture has been getting more and more fire, in the MicroServices architecture, the distributed transaction is inevitable. For the processing of distributed transaction, this paper mainly introduces a typical scheme--global transaction. But in reality, inefficient global transactions are not a good solution for distributed transaction problems in large Web sites.

In the industry, the solution used primarily to solve distributed transactions is to use flexible transactions. Flexible transactions include several types: two-stage, compensated, asynchronous-guaranteed, and maximum-effort notification. I'll have an article on the back. Flexible transactions

Welcome to 1-5 years of Java programmer friends to join the Java Architecture development: 744677563

The Group provides free learning guidance structure materials and free answers

Do not know the problem can be raised in the group after the career planning and interview guidance

Transactions in Java--Global transactions vs. local transactions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.