Full parsing of Java Transaction Processing (I) -- Basic Issues of Java Transaction Processing

Source: Internet
Author: User

How simple is transaction processing in Java? When using EJB, transactions play a role when we can hardly notice it. When using Spring, we only need to configure a TransactionManager, then add the Transactional annotation to the method that requires the transaction. Java transaction processing is so simple because the framework has done too much work for us. In this way, although we can quickly complete the development work, once the program encounters a problem, after a burst of google and stackoverflow, you may not be able to do anything. As a programmer with technical pursuits, you should understand the underlying working principles of Java transactions.

 

This is a series of articles about Java transaction processing. Please download the github source code in the following ways:

Git clone https://github.com/davenkin/java_transaction_workshop.git

 

This series of articles will explain the basic methods and principles of transaction processing in Java without relying on Spring and Java EE containers, including the following articles:

 

(1) Basic Issues of Java Transaction Processing

(2) failure cases

(3) ugly Cases

(4) successful cases (implementing a thread-safe TransactionManager)

(5) Template Mode

(6) Use a Dynamic Proxy to complete the transaction

(7) Use Transactional Annotation (Annotation) Like Spring)

(8) Introduction to distributed transactions (Spring + JTA + Atomikos + Hibernate + JMS)

 

(1) Basic Issues of Java Transaction Processing

Java interacts with databases through JDBC. This is a technology that most programmers do not directly use today. We prefer to use Hibernate and Mybatis. However, they all need JDBC to communicate with databases at the underlying layer, the same is true for transaction processing. Let's first look at the transaction processing API provided by JDBC.

 

(1) Transaction Processing API provided by JDBC

JDBC provides very few transaction processing APIs. Please do not lose confidence in the source code of the heap of transactions processed in Spring, the transaction processing functions provided by these frameworks are ultimately completed using the Connection class method:

Connection. setAutoCommit (boolean );

Connection. commit ();

Connection. rollback ();

 

In the source code of Spring's transaction processing, many of them process multiple threads, and some use some design patterns. Don't be alarmed. In this series (except Series 8), you won't see any Spring shadows. We will learn Java transactions through simple code, you can read the source code of Spring transaction processing, and then compare the transaction processing principles in this series with those in Spring. You will find that, the problems Spring faces and handles are also the problems encountered in this series of articles.

 

(2) local transactions and distributed transactions

Local Transaction refers to a Transaction involving only one data source, for example, only a database or only JMS. Distributed Transaction refers to a Transaction involving multiple data sources at the same time, for example, an operation needs to access the database and send messages through JMS at the same time, or an operation needs to access two different databases at the same time. For distributed transactions, Java provides the JTA specification. Its principles are different from those of local transactions. Given that Java transactions are local transactions in most cases, this series mainly describes local transactions, and there are introductory examples of distributed transactions in Series 8.

 

(3) thread security

Thread security is a major difficulty in Java transaction processing. For example, a DAO class maintains a Connection instance variable, and both threads use this DAO class to interact with the database at the same time, one of them closes the Connection after it is used, and the other thread is using the Connection to access the database, then the access from the other thread to the database will fail. In subsequent articles in this series, we will learn how to deal with such problems and develop thread-safe programs.

 

(4) Service layer and DAO Layer

Generally, the data persistence layer is divided into the Service layer and DAO layer. The Service layer is used to complete the work related to the business logic, and the Service layer contains the Unit of work ), that is, the methods in the Service layer are the boundaries of transactions; the DAO layer is used to complete the actual operations on the database (add, delete, modify, and query ). Sometimes, when Hibernate or JPA is used, we can directly access the database at the Service layer without deleting the DAO layer. In this series, we will use a BankService example throughout. This BankService transfers deposits from a user's Bank Account to the user's Insurance Account. The two Account pairs have different database tables.

BankService requires two DAO classes to work together. One is responsible for the operation of the bank account table and the other is responsible for the operation of the insurance account table. This is a typical transaction processing example. In the next article, we will learn a case about the failure of the BankService transaction processing.

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.