A deep understanding of the principles and applications of Java Transactions _c language

Source: Internet
Author: User
Tags rollback

One, what is Java transaction

The usual idea is that transactions are only relevant to the database.

Transactions must conform to the acid principles set out in ISO/IEC. Acid is an abbreviation for atomicity (atomicity), consistency (consistency), isolation (isolation), and persistence (durability). The atomicity of a transaction indicates that any failure in the execution of a transaction will invalidate any modifications made by the firm. Consistency indicates that all data affected by the transaction should revert to the state before the transaction was executed when the transaction execution failed. Isolation represents a modification of the data during the execution of a transaction and is not visible to other transactions until the transaction is committed. Persistence indicates that committed data should be in the correct state when a transaction fails to execute.

In layman's terms, a transaction is a group of atomic units of operation, from a database perspective, a set of SQL instructions, or all of them executed successfully, and all previously executed instructions are undone if one of the instructions is executed incorrectly for one reason or another. The simpler answer is that either the full execution succeeds or the undo does not execute.

Since the concept of a transaction comes from a database, what is the Java transaction? What's the connection?

In fact, a Java application system, if you want to manipulate the database, is implemented through JDBC. The addition, modification and deletion are realized indirectly by the corresponding method, and the control of the transaction is transferred to the Java program code accordingly. Therefore, the transactions of the database operations are customarily referred to as Java transactions.

Ii. Why Java transactions are required

Transaction is to solve the data security operation, transaction control is to control the data security access. To give a simple example: for example, bank transfer business, account A to transfer their account of the 1000 yuan under the B account, a balance of the first to subtract 1000 yuan, then B account to increase 1000 yuan. If there is a problem in the intermediary network, a account minus 1000 yuan has ended, b because the network interruption and operation failure, then the entire business failure, must make control, request a account transfer business revocation. This can guarantee the correctness of the business, the completion of this operation will require transactions, the account for a reduction of funds and B account funds to add to a transaction inside, or all of the successful implementation, or the operation is all undone, so as to maintain the security of the data.

Iii. Types of Java transactions

There are three types of Java transactions: JDBC Transactions, JTA (Java Transaction API) transactions, container transactions.

1. JDBC Transaction

JDBC transactions are controlled with the Connection object. The JDBC Connection Interface (java.sql.Connection) provides two transaction modes: Autocommit and manual submission. Java.sql.Connection provides the following methods of controlling transactions:

public void Setautocommit (Boolean) Public 
Boolean getautocommit () public 
Void commit () 
public void Rollback () 

When using a JDBC transaction definition, you can combine multiple SQL statements into a single transaction. One drawback 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, protocol-independent API that 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 the data access capability.

If you plan to define transactions with JTA, you need to have 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 the factory of a Xaconnection object. Xaconnection S is a JDBC connection that participates in JTA transactions.

You will need to set up Xadatasource with the application server's administrative tools. You can learn about instructions from the application server and the JDBC driver documentation.

The Java EE application queries the data source with JNDI. Once the application finds the data source object, it invokes Javax.sql.DataSource.getConnection () to obtain a connection to the database.

XA connections are different from non-XA connections. Be sure to remember that the XA connection participates in the JTA transaction. This means that the XA connection does not support the autocommit capability of JDBC. Also, the application must not invoke Java.sql.Connection.commit () or Java.sql.Connection.rollback () on the XA connection.

Instead, the application should use Usertransaction.begin (), Usertransaction.commit (), and Sertransaction.rollback ().

3. Container affairs

Container transactions are mainly provided by Java EE application servers, and container transactions are mostly based on JTA, a jndi-based, rather complex API implementation. Relative coding implements JTA transaction management, we can complete the same function through the container transaction management mechanism (CMT) provided by EJB container, this function is provided by Java EE Application Server. This allows us to simply specify which method to join the transaction, and once specified, the container will be responsible for the transaction management task. This is the solution to our civil work, because in this way we can exclude the transaction code from the logic code, while all the difficulties to the Java container to solve. Another benefit of using the EJB CMT is that programmers don't need to care about the coding of the JTA API, but theoretically we have to use EJBS. http://hovertree.com/menu/java/

Four or three kinds of Java transaction differences

1, the limitations of JDBC transaction control in a database connection, but its use is simple.

2, JTA transactions are powerful, transactions can span multiple databases or multiple DAO, the use is also more complex.

3, container transactions, mainly refers to the Java application Server provided by the transaction management, limited to the use of EJB applications.

V. Summary

Java transaction control is an indispensable part of the construction of EE, and it is very important for the whole application system to choose what kind of transaction to use properly. In general, you can select a JDBC transaction in the case of a single JDBC connection, and you need to choose to use a JTA transaction across multiple connections or databases, and if you use an EJB, you can consider using EJB container transactions

The above in-depth understanding of the principles and applications of Java business is small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.