Management of transactions in Java

Source: Internet
Author: User

What is a transaction:

 A transaction is a set of actions that manipulate a database.

A set of processing steps or all occurs or is not executed in one step, we call the group processing step a transaction. When all the steps are executed like an operation, we call the transaction to be committed. The transaction must be rolled back to the original system state because one or more of the steps failed, resulting in no step being committed.

The transaction must meet the acid principle. Acid is an abbreviation for atomicity (atomicity), consistency (consistency), isolation (isolation), and persistence (durability).

The atomicity of a transaction indicates that any failure during the execution of the 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 updates to committed transactions cannot be lost when a system or media failure occurs. Persistence is ensured through database backup and recovery.

Transaction processing in Java

The general Java EE Server supports three types of transaction management. That is: JDBC transaction, JTA Transaction, container management transaction.

It is best not to use the above three types of transactions in the program at the same time, and the transaction should be completed in the shortest possible time, and do not implement transaction usage in different methods. Here are two examples of JDBC and JTA transactions where container management transactions are implemented in a specific framework (e.g., Spring transaction management)

1. JDBC Transaction

public string Delete (string id) {
String id = ID;
db = new getconnection ();
Connection con = db.getconnection ();
try {
Con.setautocommit (FALSE);
Db.executeupdate ("Delete from HelloWorld where id=" + ID); Update Action 1
Db.executeupdate ("Delete from HelloWorld _book where id=" + ID); Update Action 2
Db.executeupdate ("Delete from Helloworld_user where id=" + ID); Update Action 3
Con.commit ();//Submit JDBC Transaction
Con.setautocommit (TRUE);
Db.close ();
Return "Success";
}catch (Exception e) {
Con.rollback ();//rollback JDBC Transaction
E.printstacktrace ();
Db.close ();
return "fail";
}
}

As in the example above: Update operation only if the three-step operation is completed successfully before committing, otherwise rolling back the already performed operation. This ensures the integrity of the data and does not result in data errors due to special circumstances such as sudden power outages.

2. JTA Transactions

JTA is a solution to the Java EE Transaction service and describes the Java EE model transaction interface. The JTA has three main interfaces: UserTransaction, TransactionManager, transaction. These interfaces share common transactional operations, such as commit (), rollback (). At the same time, each has its own operation. To illustrate:

public string Delete (string id) {
String id = ID;
db = new getconnection ();
Db.getconnection ();
UserTransaction transaction = Sessioncontext.getusertransaction ();//Get JTA Transaction
try {
Transaction.begin (); Start JTA Transaction
Db.executeupdate ("Delete from HelloWorld where id=" + ID);
Db.executeupdate ("Delete from HelloWorld _book where id=" + ID);
Db.executeupdate ("Delete from HelloWorld _user where id=" + ID);
Transaction.commit (); Submit JTA Transaction
Db.close ();
Return "Success";
} catch (Exception e) {
try {
Transaction.rollback ();//Transaction rollback
}catch (Exception e) {
E.printstacktrace ();
}
Exc.printstacktrace ();
Db.close ();
return "fail";
}
}

3. Container Transaction Management

In the framework of spring, hibernate and so on, have their own transaction management functions. Although the forms are somewhat different, they are implemented on the basis of Java transaction management. No introduction is made here.

Management of transactions in Java

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.