Name

Lock Manager Pattern

Goal

Provide a central point for managing lock information without requiring individual data objects to be locking-aware (Figure B-16 and Figure B-17).

Lock manager
Figure B-16. Lock manager
Offline lock manager
Figure B-17. Offline lock manager

Participants

Business delegate

Provides access to business logic.

DAO

Provides access to the persistence layer.

Lock manager

Coordinates access to resources for different objects.

Database (offline variant)

Provide a centralized location to store locking information for use by many clients or by a clustered application.

Interactions

When a business delegate or a DAO needs to lock a resource, it requests a lock on the object via a lock manager object. The lock manager takes a primary key and determines if the entity associated with the key is already locked. If it is not, it locks the resource on behalf of the user requesting the lock. When the user is done, he releases the lock.

The offline lock manager variant stores lock information “offline,” in a database. This allows multiple JVMs to share locking information in a distributed environment.

Notes

As with pessimistic concurrency in general, applications must take care to ensure that locks are released, and ensure that abandoned locks are released after a timeout period.

Get J2EE Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.