In-depth Java transaction Principles and Applications

Source: Internet
Author: User

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 JavaProgramCode. Therefore, the transaction habits of database operations are called Java transactions.

Ii. Why JAVA transactions?

Transactions are proposed to solve data security operations. In fact, transaction control controls secure data access. For example, for a 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, a transaction is required to reduce the funds of account A and increase the funds of Account B to a transaction. Either all the operations are successfully executed or all the operations are revoked, this ensures 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.

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 execute Distributed Transaction Processing-data is accessed and updated on two or more network computer resources, which can be distributed across multiple databases. The JTA support of the JDBC driver greatly enhances data access capabilities.

If you plan to use JTA to define transactions, you need a JDBC driver that implements the javax. SQL. xadatasource, javax. SQL. xaconnection, and javax. SQL. xaresource interfaces. A driver that implements these interfaces can participate in JTA transactions. A xadatasource object is the factory of A xaconnection object. Xaconnection S is the JDBC connection that participates in the JTA transaction.

You will need to use the administration tool of the application server to set xadatasource. You can learn the relevant instructions from the documents of the application server and the 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 the connection to the database.

XA connections are different from non-Xa connections. Make sure that the Xa connection is involved in the JTA transaction. This means that the Xa connection does not support the JDBC automatic submission function. At the same time, applications must not call java. SQL. Connection. Commit () or Java. SQL. Connection. rollback () for XA connections ().

On the contrary, the application should use usertransaction. Begin (), usertransaction. Commit (), and sertransaction. rollback ().

3. Container transactions

Container transactions are mainly provided by the J2EE application server, and most container transactions are completed based on JTA. This is a very complicated API implementation based on JNDI. To implement JTA transaction management through relative encoding, we can use the container Transaction Management Mechanism (CMT) provided by the EJB container to complete the same function, which is provided by the J2EE application server. This allows us to simply specify which method to add to the transaction. Once specified, the container will be responsible for the transaction management tasks. This is our civil solution, because in this way, we can exclude the transaction code from the logic code, and at the same time hand over all the difficulties to the J2EE container for solving. Another advantage of using ejb cmt is that programmers do not need to care about JTA.
API encoding. However, in theory, we must use EJB.

Iv. Differences between three Java transactions

1. Restrictions of JDBC transaction control are in a database connection, but they are easy to use.

2. JTA transactions have powerful functions. transactions can span multiple databases or multiple Dao transactions, which are also complicated to use.

3. Container transactions mainly refer to the transaction management provided by the J2EE application server, which is limited to the use of EJB applications.

V. Summary

Java transaction control is an indispensable part of J2EE application construction. It is vital for the entire application system to reasonably select the transaction to be applied. In general, you can select JDBC transactions when connecting a single JDBC connection. When connecting multiple connections or databases, you need to use JTA transactions. If EJB is used, you can consider using EJB container transactions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.