Home  >  Article  >  Java  >  How to understand the principles of Java things

How to understand the principles of Java things

WBOY
WBOYforward
2023-04-20 12:07:061281browse

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.

In layman’s terms, a transaction is a set of atomic operation units. From a database perspective, it is a set of SQL instructions. Either all of them are executed successfully. If there is an error in the execution of one of the instructions for some reason, the previous execution will be cancelled. all instructions passed. 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 customarily called Java transactions.

2. Why Java 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. Only in this way can the correctness of the business be ensured. To complete this operation, a transaction is required. The reduction 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 transaction

JDBC transaction is controlled by Connection object. 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()

When using JDBC transaction delimitation, 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.

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 across multiple databases. The JDBC driver's JTA support greatly enhances data access capabilities.

If you plan to use JTA to define transactions, you will need a JDBC driver that implements the javax.sql.XADataSource, javax.sql.XAConnection, and javax.sql.XAResource interfaces. A driver that implements these interfaces will be able to participate in JTA transactions. A XADataSource object is a factory for XAConnection objects. XAConnection s are JDBC connections that participate in JTA transactions.

You will need to set up the XADataSource using the application server's administration tools. Guidance can be found in the documentation for 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 JDBC's autocommit feature. At the same time, applications must not call java.sql.Connection.commit() or java.sql.Connection.rollback() on XA connections.

Instead, applications should use UserTransaction.begin(), UserTransaction.commit(), and serTransaction.rollback().

3. Container transaction

Container transactions are mainly provided by J2EE application servers. Container transactions are mostly completed based on JTA, which is a fairly complex API implementation based on JNDI. Compared with coding to implement JTA transaction management, we can complete the same function through the container transaction management mechanism (CMT) provided by the EJB container. This function is provided by the J2EE application server. This allows us to simply specify which method to add to the transaction, and once specified, the container will take care of the transaction management tasks. This is our civil engineering solution, because in this way we can exclude transaction code from logical coding, and at the same time leave all difficulties to the J2EE container to solve. Another benefit of using EJB CMT is that programmers do not need to care about the coding of the JTA API. However, in theory we must use EJB.

4. Differences between three types of Java transactions

1. The limitation of JDBC transaction control is within a database connection, but its use is simple.

2. JTA transactions are powerful. Transactions can span multiple databases or multiple DAOs, and their use is also complicated.

3. Container transactions mainly refer to the transaction management provided by the J2EE application server and are limited to EJB applications.

The above is the detailed content of How to understand the principles of Java things. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete