Add transaction support for servlets using JOTM (2)

zhaozj2021-02-16  50

Application design:

Data layer:

In the data layer, we need two different databases, there is a table in each database. In order to make the example more real, we will use two different databases, because the amount of money from the ATM where the bank where the user account is located is not possible. (Configure the database according to the following information)

BankTest includes an Account table that represents user account information.

Atmtest includes an ATM table that represents ATM information.

Logical layer:

At the logic layer, we have three classes to access resources and do various operations on the resource:

foo.bankAccount represents the specified user's bank account and operates through the JDBC to operate the Account table in the database.

Bar.atm represents ATM and operates with the ATM table via JDBC.

Bar.cashdelivery uses the two classes above to complete the user's money.

All logic is completed in the DeliverCash method of the CashDelivery.java class.

Javax.Transaction.usertransaction interface is used to specify whether to use transactions, all in utx.begin () and utx.commit () (or UTX.Rollback () will be completed in a transaction. This ensures that your web application will not fall into the pain discussed above.

Thanks for the business, let the application logic be so simple, just include the following steps:

1) Start the transaction;

2) Contact the user's bank and take the corresponding amount from the account;

3) Tell ATM to provide cash;

4) End the transaction

a) If all things are successful, then submit transactions;

b) Otherwise, rolling the transaction

5) Report the result of the transaction to the user. If the transaction is successfully completed, the user gets cash and the corresponding amount will be deducted on the account. Otherwise, what will not happen.

Example 1:

Cashdelivery.java

Public Boolean Deliver (String Client, Int Value) {

InitialContext CTX = New InitialContext ();

UserTransaction UTX = (Usertransaction)

CTX.lookup ("Java: Comp / UsertransAction);

...

Boolean success = false;

Try {

// Start a transaction

Utx.begin ();

/ / Communication with the user's account opening ...

BankAccount Account = New BankAccount (client);

// ... and deduct the appropriate amount from the account.

Account.withdraw (Value);

/ / Contact ATM ...

ATM ATM = New ATM ();

// ... extract cash to the user.

atm.delivercash (value);

// All things are successful.

Success = true;

} catch (exception e) {

// There is an error occurred

/ / Must provide information to users

Explanation = E.getMessage ();

} finally {

Try {

IF (Success) {

/ * All things are successful, we submit transactions.

* Only in the user account, the user account is really deducted,

* And cash is sent to the user's hand

* /

Utx.commit ();

} else {

/ * There is a mistake, let's roll back the transaction.

*

what

Do not really happen in business

* /

Utx.rollback ();

} catch (exception e) {

/ * There is an error occurred during the completion of the transaction.

* We still guarantee that the operations conducted in the transaction will not really happen.

* /

/ / We must report about information to users

Explanation = "/ n" E.getMessage ();

// Finally, the transaction is not successful

Success = false;

} finally {

RETURN SUCCESS;

}

}

}

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

New Post(0)