Detailed summary of transaction in Java
< H4 id = "1 what is a Java transaction" > < span style = "color: #3366ff;" > 1. What is a Java transaction?
The general idea is that transactions are only related to databases.
Transactions must comply with the acid principles established by ISO / IEC. Acid stands for Atomicity, consistency, isolation, and durability.
Atomicity of transaction: it means that any failure during transaction execution will invalidate any modification made by the transaction. Transaction consistency: when the transaction fails, all data affected by the transaction should be restored to the state before the transaction is executed. Transaction isolation: refers to the modification of data during transaction execution, which is not visible to other transactions before the transaction is committed. Transaction persistence: indicates that the status of the submitted data should be correct when the transaction fails.
Generally speaking, a transaction is a group of atomic operation units. From the database point of view, it is a group of SQL instructions. Either all instructions are executed successfully. If there is an error in the execution of one of the instructions for some reason, all previously executed instructions will be revoked. To put it more simply: either all execution is successful, or no execution is revoked.
< H4 id = "2 - why Java transactions" > < span style = "color: #3366ff;" > 2. Why do you need Java transactions?
Transaction is proposed to solve the problem of data security operation. Transaction control is actually to control the secure access of data.
There are three types of Java transactions: JDBC transactions, JTA (Java transaction API) transactions, and container transactions.
1. JDBC transaction JDBC transaction is controlled by connection object. The JDBC connection interface (Java. SQL. Connection) provides two transaction modes: automatic commit and manual commit.
Using JDBC transaction demarcation, you can combine multiple SQL statements into one transaction. One disadvantage of JDBC transactions is that the scope of transactions is limited to one database connection. A JDBC transaction cannot span multiple databases.
2. JTA (Java transaction API) transaction
JTA is a high-level, implementation independent and protocol independent API. Applications and application servers can use JTA to access transactions.
JTA allows applications to perform distributed transactions -- access and update data on two or more network computer resources, which can be distributed over multiple databases. The JTA support of jdbc driver greatly enhances the data access capability.
If you plan to define transactions with JTA, you need to have an implementation of javax sql. XADataSource 、 javax. sql. Xaconnection and javax sql. Jdbc driver for xaresource interface. A driver that implements these interfaces will be able to participate in JTA transactions. An xadatasource object is a factory of an xaconnection object. Xaconnections s are JDBC connections that participate in JTA transactions. You will need to set up the xadatasource with the administrative tools of the application server You can learn the relevant instructions from the documentation of the application server and jdbc driver. J2EE applications use JNDI to query data sources. Once the application finds the data source object, it calls javax sql. DataSource. Getconnection() to obtain a connection to the database. XA connections are different from non XA connections. It is important to remember that XA connections participate in JTA transactions. This means that XA connections do not support the automatic submission function of JDBC. At the same time, applications must not call java.sql.connection.commit() or java.sql.connection.rollback on XA connections (). Instead, the application should use UserTransaction. Begin(), UserTransaction. Commit(), and sertransaction. Rollback()
3. Container transactions
Container transactions are mainly provided by J2EE application server. Container transactions are mostly completed based on JTA, which is a fairly complex API implementation based on JNDI. Relative coding enables JTA transaction management,
We can complete the same function through the container transaction management mechanism (CMT) provided by the EJB container, which is provided by the J2EE application server, which allows us to simply specify which method to add
Once specified, the container will be responsible for transaction management tasks. This is our solution, because in this way, we can exclude the transaction code from the logical coding, and hand over all the difficulties to the J2EE container to solve. Another advantage of using EJB CMT is that programmers don't need to care about the coding of JTA API, but in theory, we must use EJB
1. The limitation of JDBC transaction control is within a database connection, but it is easy to use. 2. JTA transactions are powerful. Transactions can span multiple databases or multiple Daos, and their use is complex. 3. Container transaction mainly refers to the transaction management provided by J2EE application server, which is limited to EJB applications.
Java transaction control is an indispensable part of building J2EE applications. Reasonable selection of which transaction to apply is very important for the whole application system. Generally speaking, JDBC transactions can be selected in the case of a single JDBC connection. In the case of multiple connections or databases, JTA transactions need to be selected. If EJB is used, EJB container transactions can be considered.
Auto commit transaction: each individual statement is a transaction. A commit is implied after each statement. (default) explicit transaction: start with begin transaction display and end with commit or rollback.
Java JDBC transaction mechanism
First, let's take a look at what major problems the existing JDBC operation will bring us. For example, there is a business: when we modify an information, we can query the information. It is a simple business and it is very easy to implement. However, when the business is placed on a multi-threaded and highly concurrent platform, the problem naturally arises. For example, after we perform a modification, Before executing the query, a thread also executes the modification statement. When we execute the query, the information we see may be different from that we modify. In order to solve this problem, we must introduce the JDBC transaction mechanism. In fact, the code implementation is very simple. Here is a principle implementation example for your reference:
Transaction related theory
1. Four attributes (acid) of transaction
Atomic changes to data are either all or none. Consistency: the data state is consistent before and after the transaction is executed. Isolated: the processing of one transaction cannot affect the processing of another transaction. The durable transaction ends, and its effect is persisted in the database.
2. Possible problems caused by concurrent transaction processing
Dirty read: a transaction reads the uncommitted data of another transaction. Non repeatable read: the operation of one transaction causes the other transaction to read different data twice before and after phantom read: the operation of one transaction causes the result data of the two queries of the other transaction to be different.
give an example:
When transactions a and B execute concurrently, when transaction a updates, transaction B selects and reads the uncommitted data of A. at this time, transaction a rollback, then the data read by B is invalid "dirty" data. After transaction B selects and reads the data, the update operation of transaction a changes the data selected by transaction B. at this time, transaction B reads the data again and finds that the data before and after the two times are different. After transaction B selects and reads the data, transaction a inserts or deletes a record that meets the select condition of transaction A. at this time, transaction B selects again and finds that the previous non-existent record ("phantom") is queried or a previous record is missing.