JTA provides a distributed transaction service for the Java EE platform, which insulates the transaction from the underlying resources and realizes a transparent transaction management approach. This article will delve into the architecture of JTA and introduce its implementation mechanism through detailed code.
What is transaction processing
Transaction is an indispensable component model in computer application, which guarantees the atomicity (atomicity), consistency (consistency), isolation (isolation) and persistence (durabilily) of user operations. The most classic example of a transaction is credit card transfers: Transfer of 500 Yuan 9001.html in user A account to user B's account with the following procedure 1. Reduce the amount in account a by 500 2. Increase the amount in the B account by 500 Both operations must be positive for ACID's transaction attributes: either all succeed or all fail, and if there is no security, the user's account amount will be subject to problems: If the first step succeeds and the second fails, then the user a account of the amount will be reduced by 500 yuan and User B's account no increase (missing); Similarly, if the first step is wrong and the second step succeeds, then user A's account amount is unchanged and User B's account number will be increased by 500 yuan (born out of thin air). Any of these errors will result in serious data inconsistency problems, and the absence of transactions is unacceptable to a stable production system.
Java EE transaction Processing method
1. Local transactions: Closely dependent on the underlying resource manager (for example, a database connection), transaction processing is limited to the current transaction resource. This transaction is not dependent on the http://www.aliyun.com/zixun/aggregation/15818.html "> Application server, so it is flexible to deploy distributed transactions that do not support multiple data sources. Examples of using local transactions in a database connection are as follows:
Listing 1. Local transaction instance
public void Transferaccount () {Connection conn = null; Statement stmt = null; try{conn = Getdatasource (). getconnection ();//Set Autocommit to False,//If set to True the database will identify each data update as a transaction and automatically submit Conn.setautocommit (FALSE); stmt = Conn.createstatement (); Reduce the amount in account A to Stmt.execute ("\ Update t_account Set amount = amount-500 where account_id = ' a '"); Increase the amount in the B account to Stmt.execute ("\ Update t_account Set amount = Amount + where account_id = ' B '"); Submit transaction Conn.commit (); Transaction commit: Two-step operation of transfer simultaneous success} catch (SQLException Sqle) {try{///exception, rollback in this transaction do Conn.rollback ();//Transaction rollback: Two-step operation of transfer completely undo Stmt.close () ; Conn.close (); }catch (Exception ignore) {} sqle.printstacktrace (); } }
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.