Home  >  Article  >  Java  >  Summary of personal understanding of Java transactions

Summary of personal understanding of Java transactions

巴扎黑
巴扎黑Original
2017-05-22 23:50:351236browse

Transactions of database operations are customarily called Java transactions

1. What is a Java transaction

The usual concept is that transactions are only related to the database.
Transactions must comply with the ACID principles established by ISO/IEC. ACID is the abbreviation for atomicity, consistency, isolation and durability. The atomicity of a transaction means that any failure during transaction execution will cause any modifications made by the transaction to become invalid. Consistency means that when a transaction fails, all data affected by the transaction should be restored to the state before the transaction was executed. Isolation means that modifications to data during transaction execution are not visible to other transactions before the transaction is committed. Persistence means that the status of the submitted data should be correct when the transaction execution fails.
                                                                                                                                         
                                                                                                  # instruction. The simpler answer is: either all executions are successful, or they are canceled and not executed.
Since the concept of transaction comes from the database, what is a Java transaction? What's the connection?

In fact, if a Java application system wants to operate a database, it is implemented through JDBC. Addition, modification, and deletion are all implemented indirectly through corresponding methods, and transaction control is also transferred to the Java program code accordingly. Therefore, database operation transactions are traditionally called Java transactions.


2. Why transactions are needed

Transactions are proposed to solve data security operations. Transaction control is actually to control the safe access of data. Here is a simple example: For example, in bank transfer business, account A wants to transfer 1,000 yuan from its own account to account B. The balance of account A must first be subtracted by 1,000 yuan, and then the balance of account B must be increased by 1,000 yuan. If there is a problem in the intermediate network, the deduction of 1,000 yuan from A's account has ended, and the operation of B fails due to network interruption, then the entire business fails, and control must be made to require the cancellation of the transfer business of A's account. This can ensure the correctness of the business. To complete this operation, a transaction is required. The decrease of account A funds and the increase of account B funds are combined into one transaction. Either all executions are successful, or all operations are cancelled, thus maintaining the security of the data. .

3. Types of Java transactions



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 submission and manual submission. java.sql.Connection provides the following methods to control transactions:
public void setAutoCommit(boolean)
public boolean getAutoCommit()
public void commit()
public void rollback()

Use When delimiting a JDBC transaction, you can combine multiple SQL statements into a single transaction. One disadvantage of JDBC transactions is that the scope of the transaction is limited to one database connection. A JDBC transaction cannot span multiple databases.

[Related recommendations]

1. Detailed explanation of Java transaction management learning JDBC sample code

2. Java transaction management learning Detailed introduction to Hibernate

3. Java transaction management learning: Detailed code explanation of Spring and Hibernate

###

The above is the detailed content of Summary of personal understanding of Java transactions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn