Summary of personal understanding of Java transactions

  • 2020-04-01 01:33:41
  • OfStack

What is a Java transaction

The common notion is that transactions are only database related.
              Transactions must comply with the ACID principle established by ISO/IEC. ACID stands for atomicity, consistency, isolation, and durability. The atomicity of the transaction indicates that any failure in the execution of the transaction will invalidate any modifications made by the transaction. Consistency means that when a transaction fails, all data affected by that transaction should be restored to its pre-transaction state. Isolation means that data is modified during the execution of a transaction and is not visible to other transactions until the transaction commits. Persistence means that committed data should be in the correct state if the transaction fails.
              In plain English, a transaction is a set of atomic operation units, or, from a database perspective, a set of SQL instructions, all of which are executed successfully, and if for some reason one of them executes incorrectly, all of the previous instructions are revoked. The short answer is: either all successful or undo.
                Since the concept of a transaction comes from a database, what is a Java transaction? What's the connection?
              In fact, a Java application system, if you want to operate on a database, is implemented through JDBC. Adding, modifying, and deleting are done indirectly through the corresponding methods, and the control of the transaction is transferred to the Java program code accordingly. Therefore, transactions for database operations are customarily referred to as Java transactions.


Why do we need business

Transaction is proposed to solve data security operation, and transaction control is actually to control the security access of data. Take A simple example: for example, in A bank transfer business, account A will transfer 1000 yuan from his account to account b. the balance in account A will first be subtracted 1000 yuan, and then the balance in account B will be increased 1000 yuan. If there is A problem in the intermediate network, A account minus 1000 yuan has ended, B because the network interruption and operation failure, so the entire business failure, must make the control, request A account transfer business cancellation. In order to ensure the correctness of the business, the transaction is needed to complete this operation. The capital of account A will be reduced and the capital of account B will be increased into A transaction, and either all of them will be successfully executed or all of them will be canceled, so as to maintain the security of the data.

The type of Java transaction


There are three types of Java transactions: JDBC transactions, JTA (Java Transaction API) transactions, and container transactions.
1. JDBC transactions
JDBC transactions are controlled with Connection objects. The JDBC Connection interface (java.sql.connection) provides two transaction modes: automatic commit and manual commit. Java.sql.connection provides the following methods for controlling transactions:
Public void setAutoCommit (Boolean)
Public Boolean getAutoCommit ()
Public void commit ()
Public void rollback ()
With JDBC transaction demarcation, you can combine multiple SQL statements into a single transaction. One disadvantage of JDBC transactions is that they are limited in scope to a single database connection. A JDBC transaction cannot span multiple databases.


Related articles: