Reprint-java Affairs and JTA

Source: Internet
Author: User

Java transactions and JTA

First, what is a Java transaction

In layman's terms, a transaction is a set of atomic operating units , which, from a database perspective, is a set of SQL instructions, or all of them succeed, and if one of the instructions executes with an error for one reason, all previously executed instructions are revoked. The simpler answer is: either all succeeds or the revocation is not executed.

Transactions must be subject to the ACID principles established by ISO/IEC.

    • Atomicity (atomicity)
    • Consistency (consistency)
    • Isolation (Isolation)
    • Persistence (Durability)

Atomicity indicates that any failure in the execution of a transaction will invalidate any modifications made by the firm.

Consistency means that when a transaction fails, all data affected by the transaction should revert to the state it was before the transaction was executed.

Isolation represents the modification of data during the execution of a transaction and is not visible to other transactions before the transaction commits.

Persistence indicates that the submitted data should be in the correct state when the transaction execution fails.

  Since the concept of transactions comes from the database, what is a Java transaction? What is the connection between them?

In fact, a Java application, if you want to manipulate the database, is implemented through JDBC. Additions, modifications, and deletions are implemented indirectly through the corresponding methods, and the control of transactions is transferred to the Java program code accordingly. Therefore, the transactions of database operations are customarily referred to as Java transactions.


Second, why do I need Java transaction

The transaction is to give a simple example: for example, bank transfer business, account A to the account of 1000 yuan to the B account, a balance of the first account to subtract 1000 yuan, then the 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 outage and operation failed, then the entire business failure, must be control, require a account transfer business revocation. This will ensure the correctness of the business, the completion of this operation requires a transaction, the A-account capital reduction and the B-account capital increase into a transaction, or all of the successful execution, or the operation of all the revocation, so that the security of the data.


Iii. Types of Java transactions

There are three types of Java transactions:java Transaction API) transactions, container transactions.

1. JDBC Transaction

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

[Java]View PlainCopy
    1. Public void Setautocommit (boolean)
    2. Public Boolean getautocommit ()
    3. Public Void commit ()
    4. Public void rollback ()

When using JDBC transaction demarcation, you can combine multiple SQL statements into a single transaction. A 2, JTA (Java Transaction API) transaction for a JDBC transaction

JTA is a high-level, unrelated, protocol-agnostic API that enables applications and application servers to access transactions using JTA.

JTA allows an application to execute if the plan uses JTA to define the transaction, then there needs to be a javax.sql.XAResource that implements Javax.sql.XADataSource, Javax.sql.XAConnection, and JDB interfaces C driver. A driver that implements these interfaces will be able to participate in the JTA transaction. A Xadatasource object is a factory of a Xaconnection object. Xaconnections is a JDBC connection that participates in JTA transactions.

You will need to set up Xadatasource with the Application Server's administration tool. (You can learn about the guidance from the application server and the JDBC driver documentation)

The Java EE application differs from a non-XA connection by using an XA connection. Be sure to remember instead that the application should use Usertransaction.begin (), Usertransaction.commit (), and Usertransaction.rollback ().

3. Container Service

Container transactions are mainly provided by the Java EE Application Server, and the container transactions are mostly based on JTA, which is a jndi-based, fairly complex API implementation. Relative coding implements JTA transaction management, and we can accomplish the same function through the container transaction management mechanism (CMT) provided by the EJB container, which is provided by the 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 our way of doing civil work, because in this way we can exclude the transaction code from the logic code, and all the difficulties to the Java EE container to solve. Another benefit of using EJB CMT is that programmers do not have to care about the coding of the JTA API, but in theory we have to use EJBS.


Four or three types of Java transaction differences

1. The limitations of JDBC Transaction control are within a database connection , but they are simple to use.

2. JTA transactions are powerful, and transactions can span multiple databases or multiple DAO, and are more complex to use.

3, container transaction, mainly refers to the Java EE Application Server provides transaction management, limited to the use of EJB applications.


V. Summary

Java transaction control is an indispensable part of building the EE application, and it is very important for the whole application to choose what kind of transaction to apply. In general, the JDBC transaction can be selected in the case of a single JDBC connection connection, with the option to use JTA transactions across multiple connections or databases, and if EJB is used, consider using EJB container transactions.


vi. principles of JTA

There are two types of Transaction (transactions)

Local Transaction and Global Transaction
Involves a connection commit, called the local Transaction
Commits that involve multiple connection, called Global Transaction

Local transaction with JDBC Transaction implementation is not a problem, but the implementation of global transaction can not be guaranteed, so we have to understand the magic of JTA, but in fact, such a transaction is problematic, the following will point out

XA requires two-phase commit -prepare and commit.
Assuming there are two connection, Con1, Con2, the general process is as follows

[Java]View PlainCopy
  1. Con1 = Xaresouce1.getconnection ...
  2. Con2 = Xaresouce2.getconnection ...
  3. Con1 do some thing.
  4. Con2 do some thing.
  5. After they finish.
  6. Pre1 = Xaresouce1.prepare ();
  7. Pre2 = Xaresouce2.prepare ();
  8. if (both Pre1 and Pre2 are OK) {
  9. XAResouce1 and 2 commit
  10. }Else {
  11. XAResouce1 and 2 rollback
  12. }

At the time of XAResouce1 and 2 commit,
Perhaps XAResouce1 commit () succeeded, XAResouce2 commit () failed.
At this point, a "heuristic exception" is thrown. The program can handle this exception. For example, Xaresouce.recover () or something like that.
But in general, there is no other way, it is necessary for the data administrator to undo all operations according to the data operation log, or to restore the data backup.
Some databases generate an "Anti-action" log when they perform data operations. For example, insert to delete, and so on.

The Global Transaction requires support for the XA interface (including in JTA).

Import javax.sql.XAConnection;
Import Javax.transaction.xa.Xid;
Import Javax.transaction.xa.XAResource;
Import javax.transaction.xa.XAException;
Import javax.transaction.Transaction;
Import Javax.transaction.TransactionManager;

One of the
Javax.sql.XAConnection;
Javax.transaction.xa.Xid;
Javax.transaction.xa.XAResource;

The implementation of these XA interfaces requires the JDBC supply of the database.
The database itself is to support XA. The JDBC of the database also provides an implementation of XA.

The implementation of TransactionManager can handle multiple xaresouce (a xaresouce list).
Like Tyrex. or transaction implementation code for EJB server such as JBoss


Reprinted from:

"1" http://blog.csdn.net/terryzero/article/details/4464118

"2" http://blog.csdn.net/terryzero/article/details/4467739

Reprint-java Affairs and JTA

Related Article

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.