Java Reference
In-Depth Information
13.7 Using pessimistic offline locking in a domain model
In this section you will learn how to implement the Pessimistic Offline Lock pat-
tern with a lock manager. First we show the implementation of a simple lock man-
ager; after that we describe the business logic that uses it. For brevity, we'll just
discuss the domain model version of the business logic because transaction scripts
would call the lock manager in the same way.
13.7.1
Implementing a lock manager with iBATIS
The
LockManager
interface, which you saw earlier in section 13.6.5, defines meth-
ods for acquiring, verifying, and releasing locks. It is called by the transaction
scripts and domain service methods to lock and unlock objects. For the reasons
outlined earlier, the implementation of the lock manager maintains locks in the
database. It claims locks by inserting a row into the
OFFLINE_LOCK
table and
releases a lock by deleting a row:
create table OFFLINE_LOCK (
CLASS_ID VARCHAR2(100) NOT NULL,
PK VARCHAR2(100) NOT NULL,
OWNER VARCHAR2(100) NOT NULL,
CONSTRAINT OFFLINE_LOCK_PK
PRIMARY KEY (CLASS_ID, PK)
)
This table defines the following three columns:
■
CLASS_ID
identifies the type of the object being locked.
■
PK
is the primary key of the locked object.
■
OWNER
is the owner of the lock.
Its primary key consists of the
CLASS_ID
and
PK
columns.
The
LockManager
, which is shown in listing 13.3, is implemented using i
BATIS
and uses the Spring
SqlMapClientTemplate
class to access the
OFFLINE_LOCK
table.
Listing 13.3
LockManagerIBatisImpl
public class LockManagerIBatisImpl implements LockManager {
private final SqlMapClientTemplate template;
public LockManagerIBatisImpl(
SqlMapClientTemplate sqlMapClientTemplate) {
this.template = sqlMapClientTemplate;
}
private Map makeParameterMap(String classId,
Search WWH ::
Custom Search