A typical enterprise application takes data and stored data in one or more databases. These data is very important for merchants, so it is necessary to ensure its correctness, current, reliability. And when multiple programs When modifying and updating the same data, it may cause data loss. When a database update fails, the data in the database causes the data loss due to partial updates. In order to prevent the above situation, the concept of transaction processing is introduced to ensure the concept of transaction processing The integrity of data. It controls multiple programs to access data at the same time.
In order to perform transaction, there must be the following steps, and now use a bank's financial management procedure as an example:
First, start the transaction processing
1. Exctends to check the account
2. Trust savings deposit account
3. Update the log file
Second, submit a result of transaction processing
The above example must be completed in three steps, and the data will be submitted, otherwise the data will be lost. One transaction ends in both cases: Submit or recover. When a transaction is submitted, the data modification will be saved; The transaction is failed, and this transaction will be completed and the data will be restored to the previous state. Lift the above example, when it is found in the second step (trusted savings account), the interrupt is broken and the data is restored to the start State. So although the transaction failed, the data did not have an error.
EJB in J2EE is to manage transactions by the container management interface. You can set any type of EJB (Session, Entity, Messge-Drive) transactions with containers. It is very convenient to manage transactions with containers, and do not need to add in EJB. Additional code.
When a method in the EJB is called, the container manager starts a matter of a matter. It submits the transaction results when the method call is complete. Each method can set transaction processing. In a method, it is not allowed to nest or more Transaction processing.
The property of a transaction control controls its scope of itself. Such as:
Method A begins a transaction and calling method B. When the method B is executed, it is within the scope of the transaction processing of method A or a new transaction processing? This is to control the properties of transaction processing.
The properties of a transaction have: Required, Requiresnew, Mandatory, NotSupported, Supports, Never.
1, Required: When the client runs a transaction and calls a method of EJB, this method performs the client's transaction; when the client does not start a transaction, the EJB container starts a new transaction before performing this method.
2, RequiresNew: When the client runs a transaction and calls a method of EJB, the container manager does the following:
(1) The transaction processing of hanging clients;
(2) Start a new transaction;
(3) Calling method;
(4) When the method ends, the client's event is restored.
When the client does not start a transaction, the Container Manager starts a new transaction before performing this method.
3, MANDATORY: When the client runs a transaction and calls a method of EJB, this method is executed within the client's transaction processing; the container manager will throw the transactionRequiredException;
4, NotSupported: When the client runs a transaction and calls a method of EJB, the container manager terminates the transaction processing before calling the method. When the method is executed, restore the client's transaction; when the client does not start a transaction Processing, the container manager does not start the transaction when the method is called.
5, Supports: When the client runs a transaction and calls a method of EJB, execute the client's transaction processing when the method is run; when the client does not start a transaction, the container manager does not start the transaction when the method is called.
6, NEVER: When the client runs a transaction and calls a method of ejb, the container manager will throw an error (RemoteException); when the client does not start a transaction, the container manager does not start the transaction when calling the method .
Since the attributes of the transaction are stored in the configuration descriptor, they can modify them at three stages: EJB generation phase; application integration phase; configuration phase. If we can specify when configured:
You can specify a transaction property of the individual method or the entire EJB transaction property. When you specify an attribute of a transaction processing of the method, the transaction processing attribute of the method is preferred.
When the transaction failed, there are two ways to restore transaction processing of a container manager. First, if the system throws an error, the container manager automatically recovers; second, call the setrbackonly method, command the container manager to restore transaction. Below is an example:
Public void transfertosaving (double amount) throwsinsufficientBalanceException {
CHECKINGBALANCE - = Amount;
Savingbalance = Amount;
Try {
UpdateChecking (CheckingBalance);
CheckingBalance <0.00) {
Context.serollbackOnly (); // Restore
Throw new insufficientbalanceException ();
}
UpdateSaving (SavingBalance);
} catch (sqlexception ex) {
Throw new EJBEXCEPTION
("Transaction Failed Due To Sqlexception:"
ex.getMessage ());
}
}
J2EE Container Manager Controls all EJB transactions other than JDBC. It allows an EJB to update multiple databases when transaction processing. The following example is about updating multiple databases in a single transaction:
(1)
In the first example 1, the customer calls bean-a's method. This method launches a transaction, updates the database X and database Y, and calls a method in Bean-B, updating the database z in the method of bean-b, and returns Bean-a. Submit transaction processing in Bean-A.
(2)
In the above example, the client calls a method in bean-a, starting a transaction process, updating the database X, and calls the BEAN-B method of the remote J2EE, and updates the database Y. The J2EE Manager ensures that the updates of multiple databases can be completed in a transaction.