Add transaction support for servlets using JOTM (1)

zhaozj2021-02-16  28

Introduction:

J2EE provides a lot of functions for application development, but there is very few for servlets development. Servlets developers may be difficult to use these features provided by J2EE, neither not enough time to replace simple Servlets containers to a large, providing extra functionality J2EE servers. In any case

what

Using J2EE modular features, it is possible to enhance web applications by integrating some small, implementation J2EE specification functions to the servlet container, one of which is transaction.

For the full description of the J2EE transaction, you can refer to other articles. Currently, you only need to know that the transaction is the sequence operation of resources (eg, database), the characteristics of the transaction are often referred to as ACID:

Atomicity: The operation of the transaction or has succeeded (the transaction is submitted), or all failure (the transaction is rolled). This is all - all fault. A transaction should be seen as a separate unit of work. There is absolutely no completion and unfinished operation in the transaction.

Consistency: A completed transaction becomes another valid state from a valid state. A specific example of consistency is the reference consistency in the database and the primary key in the table.

Isolation: The transaction is not visible to the transaction before the transaction is submitted. Independence guarantees that the transaction does not access the resource that is modified concurrently.

Durability: Once the transaction is successfully completed, all modifications can recover from failure.

JOVA Open Transaction Manager is a full-featured, open source of ObjectWeb, which provides Java apps with transaction support and is fully compatible with JTA (Java Transaction API). You can find more details from JOTM's homepage. Integrating JTOM to Tomcat (or other servlet containers) enable JSP and Servlet developers to create a stronger web application with a lightweight approach to transactions.

To illustrate how the transaction enhances web applications, let's consider a classic, using the ATM instance of using the browser and customer interaction.

ATM instance:

Scenes:

The use case is very simple: a user wants to take money from ATM. He offers his name, john_doe, and the amount of cash he wants to take, 50 yuan. If there is enough money on his bank account, and ATM has sufficient cash, the application will provide him cash and deduct the corresponding amount from his bank account. Otherwise, the operation will be canceled, in addition to returning an error message,

what

Don't happen. In order to focus on transaction processing and make things simple, we don't need to care about security issues, assuming users have passed security certification.

This very simple example is difficult to achieve strong sense of strongness without business support. A client application includes operations for two different resources: ATM and user bank accounts. In the application design, it will naturally propose ACIITY problems. For example, if the operation of the ATM is successful and the operation of the bank account fails (possibly due to communication failure), the user will take the cash and the bank account will not be updated, for the bank It is a bad news.

Unfortunately, if the bank account update is correct and ATM provides an error, the user does not get cash, but his bank account will deduct the corresponding amount.

In order to prevent the above things, in your application, you can: 1) Connect two resources and notify them all the operations currently in progress;

2) Ask if they can do this, and ...;

3) If they all recognize, they require them to perform operations.

If another operation is in the second, three, from the user's account, even if the above solution is still not strong enough, cash extraction errors may occur, for example, if the user's bank account does not allow negative numbers Happening.

Here is that the transaction can make your app more simple and strong. By putting all the operations of the two resources in the same transaction, you can solve the ACIITY problem.

转载请注明原文地址:https://www.9cbs.com/read-27639.html

New Post(0)