Summary of Java transactions
1. What is a Java transaction?
Generally, transactions are only related to databases.
Transactions must follow the ACID principles established by ISO/IEC. ACID is short for atomicity, consistency, isolation, and durability. The atomicity of a transaction indicates that any failure during the transaction execution will invalidate any modification made by the transaction. Consistency indicates that when the transaction fails, all data affected by the transaction should be restored to the status before the transaction is executed. Isolation indicates the modification of data during transaction execution, which is invisible to other transactions before the transaction is committed. Durability indicates that the data status should be correct when the transaction fails.
In a general sense, a transaction is a group of atomic operation units. From the database perspective, it is a group of SQL commands, or all of them are successfully executed. If one of the commands is incorrect for some reason, undo All commands previously executed. To put it simply, either the execution is successful or the execution is canceled.
Since the concept of transactions comes from the database, what is a Java transaction? What is the relationship between them?
In fact, a Java application system uses JDBC to operate databases. Adding, modifying, and deleting are implemented indirectly through corresponding methods, and transaction control is also transferred to Java program code. Therefore, the transaction habits of database operations are called Java transactions.
Ii. Why transactions are required?
Transactions are proposed to solve data security operations. In fact, transaction control controls secure data access. A simple example is provided: for example, in the bank transfer business, account A must transfer the RMB 1000 from his account to account B. The balance of account A must first be reduced by RMB 1000, then, Account B needs to add 1000 yuan. If A problem occurs in the intermediate network, the operation of account A minus 1000 yuan has ended, and account B fails due to network interruption, the entire business fails and must be controlled, requiring account A to cancel the transfer business. This ensures the correctness of the business. To complete this operation, you need to perform A transaction to reduce the funds of account A and increase the funds of Account B to A transaction, or execute all the operations successfully, you can also revoke all operations to ensure data security.
Iii. Java transaction types
There are three types of Java transactions: JDBC transactions, JTA (Java Transaction API) transactions, and container transactions.
1. JDBC transactions
JDBC transactions are controlled by 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 to control transactions:
Public void setAutoCommit (boolean)
Public boolean getAutoCommit ()
Public void commit ()
Public void rollback ()
When you use the JDBC transaction interface, you can combine multiple SQL statements into one transaction. One disadvantage of JDBC transactions is that the transaction scope is limited to a database connection. A jdbc transaction cannot span multiple databases.