Java Reference
In-Depth Information
■
RestaurantNotificationGateway
, which encapsulates the mechanism for
sending orders to restaurants
■
OrderDAO
, which encapsulates the database access code
■
OrderDAOIBatisImpl
, which implements the
OrderDAO
interface using the
Spring
SqlMapClientTemplate
, a wrapper around the i
BATIS
classes
■
OrderDTO
, which represents an order
■
TransactionInterceptor
, which is the Spring
AOP
interceptor that manages
transactions and
JDBC
connections
RestaurantNotificationTransactionScripts
defines the
sendOrders()
method,
which is the transaction script that sends the orders to the restaurant. This
method first calls the
OrderDAO
to find the orders to send. It then sends each order
using the
RestaurantNotificationGateway
. After sending the orders, it calls the
OrderDAO
to mark the orders as having been sent.
The
OrderDAO
defines a
findOrdersToSend()
method, which returns a list of
orders, and a
markOrdersAsSent()
method, which updates the orders to indicate
that they been sent. The
OrderDAOIBatisImpl
class uses a Spring
SqlMapClientTem-
plate
to execute the
SQL
statements that are defined in the i
BATIS
file Order.xml.
Let's now look at how this business logic can use each of the database concur-
rency mechanisms. In a real application, the business logic for a use case would
only use one concurrency mechanism—most likely optimistic locking—but it is
educational to compare the implementations of each one.
12.2.2
Using optimistic locking
The easiest way to implement optimistic locking is to add a version column to the
PLACED_ORDER
table, which is incremented by the application each time it
updates a row:
CREATE TABLE PLACED_ORDER (
…
VERSION NUMBER(10) DEFAULT 0 NOT NULL,
…
)
The version column along with the rest of the data from the
PLACED_ORDER
table is retrieved by the
SQL
SELECT
statement executed by the
OrderDAO
:
SELECT * … o.
VERSION
…
FROM PLACED_ORDER o, RESTAURANT r, PLACED_ORDER_LINE_ITEM l
WHERE
bb
o.status = 'PLACED' AND DELIVERY_TIME < ?
Search WWH ::
Custom Search