How To / Locking Strategies |
This covers how locking works with the JDBC Engine, and how you can programmatically control
the type of locking you want in various situations.
- Locking Basics
- Choosing the Lock
|
Locking Basics |
The persistence engine supports the following locking strategies for rows retrieved from the database.
- Optimistic
This is the default locking strategy. The record in the database will be locked, only when it is updated/deleted by the persistence engine.
This can happen anytime between a uow.add(aDomainObject) [or uow.update(aDomainObject) or uow.delete(aDomainObject)] and a uow.commit()
- Cautious
The record in the database will be locked, whenever a field is updated on the domain object. Invocation of the updateXxx method on a domain object, which is retrieved from the database and which is not locked as yet, will lock the corresponding row in the database.
- Paranoid
The record in the database will be locked, at the instant it is retrieved.
- ReadOnly
The record in the database will never be modified. Invocation of the updateXxx method on the domain object, will throw ReadOnlyObjectException
|
Choosing the Lock |
The locking strategy is specified in the Criteria object used for querying the database.
Typical code: |
UOW uow = null;
try {
uow = new UOW();
// retrieve the domain object(s), providing the appropriate locking strategy
Criteria c = new Criteria();
...
c.setLocking(Criteria.LOCKING_PARANOID);
Collection results = uow.query( c );
for (Iterator itr = results.iterator(); itr.hasNext(); ) {
...
...
}
...
...
} catch (UOWException e) {
// handle the exception
} finally {
if (uow != null)
uow.rollback();
}
|
|