Java Transaction Processing

Source: Internet
Author: User

A What is a transaction
A transaction is a rigorous set of operations in an application, and all operations must be completed successfully, or all changes made in each operation will be undone. That is, transactions are atomic, and a series of operations in a transaction are either successful or not.
The end of a transaction has two types, and when all the steps in the transaction are executed successfully, the transaction commits. If one of the steps fails, a rollback operation occurs, and the undo action is undone until the start of the transaction.
Two ACID of the transaction
A transaction has four characteristics: atomicity (atomicity), consistency (consistency), isolation (isolation), and persistence (durability). These four properties are referred to as ACID properties.
1. Atomic Nature
A transaction is a logical unit of work for a database, and each operation contained in a transaction is either done or not
2. Consistency
The result of the transaction execution must be to change the database from one consistent state to another. Therefore, when the database contains only the results of successful transaction commits, the database is said to be in a consistent state. If a database system fails in operation and some transactions have not yet been completed, some of the modifications made to the database have been written to the physical database, and the database is in an incorrect state, or an inconsistent state.
3, the isolation of
Execution of one transaction cannot interfere with other transactions. That is, the operations within one transaction and the data used are isolated from other concurrent transactions, and the transactions performed concurrently cannot interfere with each other.
4, the continuity of
Also known as permanence, when a transaction is committed, its changes to the data in the database should be permanent. The next operation or failure should not have any effect on its execution results.

Transaction processing in Java

In general, the Java EE Application Server supports JDBC transactions, JTA (Transaction API) transactions, and container management transactions. In general, it is best not to use the above three types of transactions in the program, such as nesting JDBC transactions in JTA transactions. Second, the transaction should be completed in the shortest possible time, and do not implement transaction usage in different methods. Below we list two kinds of transaction processing methods

1. Using JDBC in JavaBean for transaction processing
How do you combine multiple SQL statements into a single transaction in JDBC? In JDBC, when you open a Connection object connection, the default is Auto-commit mode, and each SQL statement is treated as a transaction, that is, each time a statement is executed, it is automatically acknowledged by the transaction. In order to be able to combine multiple SQL statements into a single transaction, the Auto-commit mode is masked out. If the commit () method is not called after the Auto-commit mode is masked, the SQL statement will not receive a transaction acknowledgment. All SQL after the last commit () method call is confirmed when the method commit () is called.

public int Delete (int sID) {DBC = new databaseconnection (); Connection con = dbc.getconnection ();  try {Con.setautocommit (false);//Change the default submission mode for JDBC Transactions dbc.executeupdate ("Delete from bylaw where id=" + SID);  Dbc.executeupdate ("Delete from bylaw _content where id=" + SID);  Dbc.executeupdate ("Delete from bylaw _affix where bylawid=" + SID);  Con.commit ();//Submit JDBC Transaction con.setautocommit (TRUE);//Restore the default submission mode for JDBC transactions dbc.close (); return 1;  } catch (Exception exc) {con.rollback ();//rollback JDBC Transaction exc.printstacktrace ();  Dbc.close (); return-1; }}

The JTA transaction in

2, Sessionbean
JTA is the Java EE Solution for transactional services. Essentially, it is part of the Java EE model that describes a transactional interface, such as a usertransaction interface, that the developer uses directly or through the Java EE container to ensure that the business logic can run reliably. The JTA has three main interfaces, namely the UserTransaction interface, the TransactionManager interface, and the Transaction interface. These interfaces share common transactional operations, such as commit () and rollback (), but also contain special transactional operations such as suspend (), resume (), and Enlist (), which appear only on specific interfaces to allow a certain degree of access control in the implementation. For example, usertransaction can perform transactional partitioning and basic transactional operations, while TransactionManager can perform context management. The

application can call the Usertransaction.begin () method to start a transaction that is associated with the current line threads in which the application is running. The underlying transaction manager actually handles the association between the thread and the transaction. The Usertransaction.commit () method terminates the transaction associated with the current thread. The Usertransaction.rollback () method discards the current transaction associated with the current thread.

public int Delete (int sID) {databaseconnection DBC = null; DBC = new DatabaseConnection (); Dbc.getconnection ();  UserTransaction transaction = Sessioncontext.getusertransaction ();//Get JTA Transaction try {transaction.begin ();//Start JTA Transaction  Dbc.executeupdate ("Delete from bylaw where id=" + SID);  Dbc.executeupdate ("Delete from bylaw _content where id=" + SID);  Dbc.executeupdate ("Delete from bylaw _affix where bylawid=" + SID); Transaction.commit ();  Submit JTA Transaction dbc.close (); return 1; } catch (Exception exc) {try {transaction.rollback ();//JTA transaction rollback} catch (Exception ex) {//JTA transaction rollback error Handling Ex.prints  Tacktrace ();  } exc.printstacktrace ();  Dbc.close (); return-1; }}

The above code from the Internet, if there are errors, enjoy understanding!

Java Transaction Processing

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.