Transaction strategy: understand transaction traps — forward
Common errors to pay attention to when implementing transactions in the Java platform
Transactions are often used in applications to maintain a high degree of data integrity and consistency. If you don't care about the quality of the data, you don't have to use transactions. After all, transaction support in the Java platform will reduce performance, cause locking problems and database concurrency problems, and increase the complexity of applications.
But developers who don't care about business get into trouble. Almost all business-related applications require a high degree of data quality. The financial investment industry wasted tens of billions of dollars on failed transactions, Bad data is the second major factor leading to this result (see). Although the lack of transaction support is only one factor leading to bad data (but it is the main factor), it can be considered that billions of dollars are wasted in the financial investment industry due to the lack of transaction support or insufficient transaction support.
Ignoring transaction support is another cause of the problem. I often hear statements like "we don't need transaction support in our applications because these applications never fail". Yes, I know some applications that rarely or never throw exceptions. These applications are based on well written code, well written verification routines, fully tested, and supported by code coverage, which can avoid performance loss and transaction related complexity. This type of application only needs to consider one feature of transaction support: atomicity. Atomicity ensures that all updates are treated as a single unit, either committed or rolled back. However, rollback or simultaneous updates are not the only aspect of transaction support. On the other hand, isolation will ensure that one unit of work is independent of other units of work. Without proper transaction isolation, other units of work can access updates made by an active unit of work, even if the unit of work has not completed. In this way, business decisions will be made based on some data, which will lead to failed transactions or other negative (or expensive) results.
Therefore, considering the high cost and negative impact of bad data, And the importance of business (and necessity) for these basic common sense, you need to use transaction processing and learn how to deal with possible problems. Many problems often occur after you add transaction support to your application. Transactions do not always work as expected in the Java platform. This article will explore the reasons for this. I will use code examples to introduce some of the problems I continue to see in this field And experienced common transaction traps, mostly in production environments.
Although most of the code examples in this article use the spring framework (version 2.5), the transaction concept is the same as that in the EJB 3.0 specification_ cnnew1@TransactionAttribute Replace the spring framework @ transactional annotation with the annotation. If the two frameworks use different concepts and technologies, I will give both spring framework and EJB 3.0 source code examples.