Java Theory and Practice: An Introduction to Understanding jts--transactions

Source: Internet
Author: User

The Java Transaction Service is a key element of the EE architecture. Combined with the Java Transaction API, it enables us to build distributed applications that are very robust for a variety of system and network failures. Transactions are a basic building block for reliable applications--it will be very difficult to write reliable distributed applications without the support of a transaction. Fortunately, most of the work performed by JTS is transparent to programmers, and the Java container makes transaction partitioning and resource expropriation almost invisible to programmers. This first installment of the three-part series describes some of the basics, including what transactions are and why transactions are critical to building reliable, distributed applications.

If you have read any introductory articles or books about Java EE, you will find that only a small portion of the information is specifically targeted at the Transaction Service (JTS) or Java Transaction API (JTA). This is not because JTS is an unimportant part or an optional part of the Java EE-quite the opposite. JTS is less focused than EJB technology because it provides a very transparent service to applications-many developers don't even notice where transactions begin and end in their applications. In a sense, JTS's obscurity is precisely its success: because it hides many of the details of transaction management very effectively, we have not heard or talked much about it. However, you might want to know what it does for you behind the scenes.

It is no exaggeration to say that a reliable distributed application cannot be written without a transaction. Transactions allow some control over the persistence state of the application to make the application more robust for a wide variety of system failures, including system crashes, network failures, power failures, and even natural disasters. Transactions are one of the basic building blocks needed to build fault-tolerant, high-reliability, and high-availability applications.

Motives of the business

Suppose you are transferring from one account to another account the difference is represented by a row in the database table. If you want to transfer from account A to account B, you may execute the following SQL code:

SELECT accountBalance INTO aBalance
   FROM Accounts WHERE accountId=aId;
IF (aBalance >= transferAmount) THEN 
   UPDATE Accounts
     SET accountBalance = accountBalance -  transferAmount
     WHERE accountId = aId;
   UPDATE Accounts
     SET accountBalance = accountBalance +  transferAmount
     WHERE accountId = bId;
   INSERT INTO AccountJournal (accountId, amount)
     VALUES (aId, -transferAmount);
   INSERT INTO AccountJournal (accountId, amount)
     VALUES (bId, transferAmount);
else
   FAIL "Insufficient funds in account";
END if

So far, this code looks very straightforward. If sufficient funds are available, subtract the funds from one account and add them to another account. However, what happens if a system power failure or crash occurs? Lines representing account A and account B may not be stored in the same disk block, which means that multiple disk IO is required to complete the transfer. What happens if the system fails after writing the first disk block before the second disk block is written? The funds in account a have been crossed, but not in account B (A and B will not be willing), or the funds will appear in Account B, but not credited to account A transfers (the bank will not). What happens if the account is updated correctly and the account journal is not updated? Then the monthly bank statements for account A and account B will be inconsistent with the balance of their accounts.

Not only is it not possible to write multiple blocks of data to disk at the same time, but it also has a detrimental effect on system performance when each block of data is written to disk as soon as the modifications are made. Delaying disk writes to a more appropriate time may significantly improve the throughput of your application, but it needs to be done in a way that does not compromise data integrity.

Even in the absence of a system failure, the code discussed above has another risk-concurrency. What happens if account A has 100 dollars, but at the same time it starts transferring 100 dollars to its two different accounts? If time is coincidentally, and no proper locking mechanism is in place, two transfers can be successful, resulting in a negative balance in account A.

These things seem to be very likely to happen, so it is desirable for enterprise data systems to solve these problems. We expect banks to maintain proper account records in the event of fires, floods, power failures, disks, and system failures. Fault tolerance can be provided through redundancy (redundant disks, computers, and data centers), but transactions make it possible to build fault-tolerant software applications. Transactions provide a framework for maintaining data consistency and integrity in the event of a system or component failure.

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.