Add transaction support for servlets using JOTM (3)

zhaozj2021-02-16  62

Indicate layer:

In the representation layer, composed of two JSP files:

ATM.JSP: Cash support application, used to send user information and the amount of cash required to take into the bar.cashdelivery class, and display the result of the user's operation.

Admin.jsp: Management Console is used to display and update information related to two resources. (Not part of the programming, but you must add to make resource update operations simple, such as depositing money to user accounts)

Figure 1 Application design

Configure the database:

About the database, we use mysql

4.0.12

And the included JDBC driver. MySQL defaults do not provide transaction support, in order to support transactions, you must declare the InnoDB type when you create a table. In addition, in order to provide INNODB type support, you must annotate # skip-innodb in the mysql configuration file (My.cnf).

In this example, the mysql user is set for Javauser, the password is Javaude, and you need to confirm that the user has been created and has permissions to create a database.

Create a database and a script of the table in the scripts / directory of the sample file. Running the script file will create an Account table and insert two user records:

There are 100 yuan on the John_Doe account.

600 yuan on the Jane_Doe account.

Example 2: Creating an Account Table

Mysql> Create Database BankTest;

Mysql> Use BankTest;

Mysql> Create Table Account

-> Client varchar (25) Not Null Primary Key,

-> Money int) TYPE = INNODB;

Mysql> Insert Into Account Values ​​("John_Doe", 100);

Mysql> Insert Into Account Values ​​("Jane_Doe", 600);

Mysql> Select * from account;

-------- -------

| CLIENT | MONEY |

-------- -------

| john_doe | 100 |

| jane_doe | 600 |

-------- -------

The script file also creates an ATM table and provides a record that can take 500 yuan in cash:

Example 3: Creating an ATM Table

Mysql> CREATE DATABASE ATMTEST;

mysql> Use atmtest;

Mysql> Create Table ATM

-> ID INT NOT NULL AUTO_INCREMENT Primary Key,

-> cash int) TYPE = INNODB;

Mysql> Insert Into ATM Values ​​(NULL, 500);

Mysql> Select * from ATM;

-- ------

| ID | CASH |

-- ------

| 1 | 500 |

-- ------

Finally, you need to copy JDBC to drive to $ Tomcat installation directory $ / share / lib directory.

Get and install Tomcat

This article is based on Tomcat

4.1.18

And the above version is written, please confirm that you have not used the previous version, install Tomcat is not any

what

Special, you can only download and decompress it. Get and install JOTM

In order to use JOTM, you must download and decompress from the latest binary release, copy * .jar file from the lib directory (except log4j.jar, common-cli.jar and jotm_iiop_stubs.jar) to $ Tomcat installation directory $ Under the Shared / Lib directory, everything is completed.

Configure Tomcat

You now need to configure Tomcat to enable it to get UserTransaction and DataSource objects from JNDI (for foo.bankaccount and bar.atm).

First, tell Tomcat what JNDI name is used in your web application to find the data source. This work is done in the web.xml file, and the contents of the web.xml file are listed below. For bank account data sources, JNDI names are Java: Comp / Env / JDBC / BankAccount, but you only need to give the name after Java: Comp / Env /. Tomcat uses the JNDI mechanism to parse the contents of the remaining part. Also processed for ATM data sources.

Example 4 4: Web.xml

Bank Account DataSource

JDBC / BankAccount

javax.sql.datasource

ATM Datasource

JDBC / ATM

javax.sql.datasource

Now you need to tell Tomcat

what

Retrieves the resources in web.xml, which is done in the Bank.xml file, and the contents of the Bank.xml file are listed below. For bank accounts and ATM resources, the correct parameters must be set to Tomcat links the data source to your web application. More detailed descriptions can be found in the Tomcat Jndi How-To Document.

There is a parameter very special: Factory. Use this parameter setting class to create a data source when you find JNDI at a web application. Another important resource described in Web.xml is UserTransaction. This resource is used by Java: Comp / UsertransAction to distinguish whether transaction support is used. The implementation of this resource is provided by JOTM.

Example 5: Bank.xml

factory

Org.ObjectWeb.jndi.DataSourceFactory

URL

jdbc: mysql: // localhost / BankTest

o Username - Name of Database User

o Password - Password of the Database User

o Driverclassname - JDBC Driver Name

->

...

...

factory

Org.ObjectWeb.Jotm.UserTransActionFactory

jotm.timeout

60

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

New Post(0)