In addition to Servlet, J2EE also provides a large number of other functions. Servlet developers may rarely use these features, and are reluctant to use a large J2EE server that exceeds the required time to replace their own simple Servlet container. However, based on the modular features of J2EE, it is possible to integrate small components responsible for specific J2EE functions into Servlet containers to Enhance WEB applications. One of them is a transaction. For a complete description of J2EE transactions, you can refer to the other three articles in Onjava. Now you only need to know that the transaction is a resource operation step (for example, a database), which is defined by four attributes, the four attributes are condensed into ACID according to the first letter:
Atomicity: the transaction operation, whether it is all successful (the transaction is committed at this time), or all failed (the transaction is rolled back at this time), which is called the all-or-nothing attribute. A transaction should be regarded as a single unit of work. It is absolutely impossible for a transaction to have completed and unfinished operations at the same time.
Consistency: completed transactions change resources from one valid state to another valid state. Examples of consistency include the integrity of the database and the uniqueness of the primary key in the table.
Before the transaction is committed, changes to the shared resources used by the transaction are invisible outside the transaction. Independence ensures that different transactions do not simultaneously access the data being updated.
Persistence: Changes committed by transactions will exist permanently.
JOTM (Java Open Transaction Manager) is an independent Transaction Manager with complete functions and Open resources developed by the ObjectWeb Association. It supports JAVA application transactions and is compatible with JTAJAVA transaction APIs. You can learn more on JOTM home page. After TOMCAT or other Servlet containers integrate JOTM, JSP and Servlet developers can easily create more robust web applications with the advantages of transactions.
To highlight how transactions enhance web applications, a common example is the ATM used by web browsers to interact with clients.
ATM example:
Scenario
This example is relatively simple: a customer wants to withdraw money from an ATM and enters his customer name, john_doe; number of withdrawals, $50. If his bank account has enough money and enough cash on the ATM, the application can give him a considerable amount of cash and propose the same amount from the bank account. Otherwise, the operation is interrupted and will not change except for the error message. We don't need to worry about security issues. We just want to guess whether users are authorized correctly.
This is a very simple example, but if you do not use transactions, it will be difficult to execute them in other ways. Client operations involve two different resources: ATM and customer bank account. They automatically generate ACID Problems in Application Design. For example, if the operation succeeds on the ATM but fails on the bank account, the communication may fail.) The customer will get the money, but his account will not be updated. This is a big loss for banks. Even worse, if the bank account is updated, but the client cannot get the cash due to an error that prevents money from being transferred by the ATM, the money is removed from the account.
In order to prevent the above incidents, in your application, you can 1) contact two resources and notify the two customers of all the current operations, 2) ask whether the two can perform the operations, 3) if both of them agree, request the operation. Even so, this method cannot be said to be robust enough, because if the money in the Customer Account is extracted by another operation in step 2 and step 3, the withdrawal may fail, for example, the customer account cannot have a deficit.
Transactions make the application simpler and more robust: When you execute all the operations on the two resources of the same transaction, it will solve the ACID problem, especially atomicity ).
- Scala Servlet in Scala Language
- Servlet API Discussion
- Introduction to Servlet containers and Context
- Servlet Source file to Class Process
- Details about Listener listening to Http sessions